Async Job Queue Processor Scheduler Job


Step 1: Create Step Class

At Breezz Step we can delay execution of code logic using Scheduler. We will use the same method as in the Async Trigger example here.

What did we have in our Step class?

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

Step 2: Create Test Class

What did we have in our Step test class?

 1@IsTest
 2private class ProcessingCreateContactTest {
 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 ProcessingCreateContact 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 ProcessingCreateContact 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 ProcessingCreateContact())
21                .execute(accs);
22
23        forvendi.BreezzApi.TESTS.deliverAsyncQueueEvents();
24        Test.stopTest();
25
26        // Perform a SOQL query to retrieve the contacts associated with the accounts
27        List<Contact> contacts = [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accs];
28
29        // Assert that the number of contacts created matches the number of accounts created
30        Assert.areEqual(2, contacts.size());
31    }
32}

This class is pinned to trigger by Step. We already configured this Step here.

After creating an Account id of this will be added to the queue by addAsyncJob. Records ids are stored in a queue before running the scheduler. Contact will be created for every Account after executing this Step from Scheduler. After successfully running the scheduler, the queue will be cleaned.


Step 3: Modify Step

Now we need to modify our Step. Go to Breezz SetupStep Groups → Select Account_AIProcessingCreateContactEdit

Go to Async Configuration:

  • Async Strategy set to Multi Queueable
  • Is Queued set as Queued
  • Realtime Processing set to Wait For Scheduler

Async Configuration Async Configuration

Click Save and proceed with the next step.


Step 4: Check Scheduler Job

To check if a scheduler has been created go to App LauncherBreezzBreezz Scheduler JobsAll Custom Jobs.

Here you can see your Scheduler:

GenerateContactScheduler record GenerateContactScheduler record

However, if scheduler record is not present on that list, re-run Scheduler.

You can re-run Scheduler manually from apex. Using Developer ConsoleOpen Execute Anonymous Window:

forvendi.BreezzApi.SCHEDULER.rerun();

Or you can Run Manually in Scheduler Jobs Settings in Scheduler Jobs:

Run Scheduler Jobs Manually Run Scheduler Jobs Manually