Delayed Async Execution Example


Using the Breezz Framework we can delay execution of code logic which we implemented in Step.

In this example we will use after insert trigger on Account. We created it in the previous examples. You can check that here.


Step 1: Create Apex Trigger and Trigger Config

Now go to o Breezz Setup → Triggers and check if you have already created Account_AI. If not, configure that trigger. You can check that here.


Step 2: Create Step Class

Let’s start from Step class creation. You can read more about this here.

 1public class DelayedContactGenerator extends forvendi.Step {
 2   
 3    public DelayedContactGenerator() {
 4        super(DelayedContactGenerator.class.getName());
 5    }
 6 
 7    public override Boolean initRecordProcessing(Object record, Object optionalOldRecord) {
 8        // adding incoming record to the DelayedAsyncJob
 9        Account accountRecord = (Account)record;
10        addDelayedAsyncJob(accountRecord.Id, System.now());
11        return false;
12    }
13 
14    public override void executeAsyncProcess(Map<String, forvendi.AsyncJobInfo> asyncJobsByRecordKey) {
15        // for every record added in DelayedAsync insert new contacts
16        for (String record : asyncJobsByRecordKey.keySet()) {
17            getContext().addToInsert(new Contact(LastName = 'Contact ' + System.now(), AccountId = record));
18        }
19    }
20}

After the account is created, its id will be added to the DelayedAsyncJob. After 30 minutes(It’s default value but you can change this in Breezz Setup → Scheduler Setup → Delayed Jobs Processing Configuration) it will be asynchronously launched, that means, the logic will be done inside the executeAsyncProcess method.


Step 3: Create Test Class

You can read more about how to test Step here.

 1@IsTest
 2private class DelayedContactGeneratorTest {
 3
 4    @TestSetup
 5    static void testSetup() {
 6        // initializing Breezz setup,
 7        // you have to provide forvendi.BreezzPlugins.BaseApexPlugin implementation if you would like to
 8        // use public classes like DelayedContactGenerator in Breezz
 9        forvendi.BreezzApi.TESTS.init('BreezzPlugin');
10    }
11   
12    @IsTest
13    static void when_ExecuteContactGenerator_expect_GenerateContactForEveryAccount() {
14        // after inserting accounts force synchronous execution of DelayedContactGenerator class
15        Account[] accs = new Account[]{ new Account(Name = 'new account 1'), new Account(Name = 'new account 2')};
16        insert accs;
17
18        Test.startTest();
19        forvendi.ModificationContext ctx = forvendi.BreezzApi.STEPS.build()
20                .addStep(new DelayedContactGenerator())
21                .execute(accs);
22
23        forvendi.BreezzApi.TESTS.deliverAsyncQueueEvents();
24        forvendi.BreezzApi.TESTS.deliverDelayedAsyncJobs();
25        forvendi.BreezzApi.TESTS.deliverAsyncQueueEvents();
26        Test.stopTest();
27
28        // Perform a SOQL query to retrieve the contacts associated with the accounts
29        List<Contact> contacts = [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accs];
30
31        // Assert that the number of contacts created matches the number of accounts created
32        Assert.areEqual(2, contacts.size());
33    }
34}

Step 4: Register Step

To create Step go to Breezz Setup → Step Groups → Account_AI → Click New Step. Configure New Step Configure New Step

In Step Class Name choose previously created class: DelayedContactGenerator. Choose the name, for our purpose it will be DelayedContactGenerator. Add some Description. Next input Order of execution.


Step 5: Test Delayed Async execution

How does it work? Breezz has a special job that will be triggered every 30 minutes. It collects all previously added jobs via addDelayedAsyncJob and adds them to the asynchronous jobs that will be executed later.

First, you need to go to Breezz Setup → Scheduler Setup and on the right check if Scheduler Status is active. You should see something like this:

Scheduler Status Scheduler Status

After completing all previous steps go to App Launcher → Accounts → create a new Account go to the right corner click New → Input name and click save.

Account Information Account Information

We can see Delayed jobs in Force App. To check this go to Breezz → On the navigation panel select Breezz Delayed Async Jobs

Breezz Delayed Async Jobs Breezz Delayed Async Jobs

After 30 minutes from the last execution time, Contact on this account will be created automatically. To check last execution time go to Breezz → On the navigation panel select Breezz Scheduler Jobs.

Breezz Scheduler Jobs Breezz Scheduler Jobs