Async Step Execution Example
Utilizing the Breezz Framework, the code logic implemented in Steps can be executed asynchronously.
Step 1: Create Apex Trigger and Trigger Config
In this example we will use after insert trigger on Account. We created them in one of the previous examples. You can check that here.
Now we need to configure a new Trigger. Go to Breezz Setup → Triggers → New
For the Trigger Event choose After Insert and in the Name field should be autocomplete on Account_AI.
Step 2: Create Step Class
Let’s start from Step class creation. You can read more about this here.
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}
After the account is created, its id will be added to the AsyncJob. It will be asynchronously launched, that means, the logic will be done inside the executeAsyncProcess method.
Step 3: Create a test method for the Step class.
1@IsTest
2private class ProcessingCreateContactTest {
3
4
5 @TestSetup
6 static void testSetup() {
7 // initializing Breezz setup,
8 // you have to provide forvendi.BreezzPlugins.BaseApexPlugin implementation if you would like to
9 // use public classes like ProcessingCreateContact in Breezz
10 forvendi.BreezzApi.TESTS.init('BreezzPlugin');
11 }
12
13
14 @IsTest
15 static void when_ExecuteContactGenerator_expect_GenerateContactForEveryAccount() {
16 // after inserting accounts force synchronous execution of ProcessingCreateContact class
17 Account[] accs = new Account[]{ new Account(Name = 'new account 1'), new Account(Name = 'new account 2')};
18 insert accs;
19
20
21 Test.startTest();
22 forvendi.ModificationContext ctx = forvendi.BreezzApi.STEPS.build()
23 .addStep(new ProcessingCreateContact())
24 .execute(accs);
25
26
27 forvendi.BreezzApi.TESTS.deliverAsyncQueueEvents();
28 Test.stopTest();
29
30
31 // Perform a SOQL query to retrieve the contacts associated with the accounts
32 List<Contact> contacts = [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accs];
33
34
35 // Assert that the number of contacts created matches the number of accounts created
36 Assert.areEqual(2, contacts.size());
37 }
38}
Step 4: Register Step
To create Step go to Breezz Setup → Step Groups → Account_AI → Click New Step.
In Step Class Name choose previously created class: ProcessingCreateContact. Choose the name, for our purpose it will be ProcessingCreateContact. Add some Description. Next input Order of execution.
Step 5: Test Async Step execution
How does it work? Breezz collects all previously added jobs via addAsyncJob and adds them to the asynchronous jobs that will be executed..
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:
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.
Contact should be created automatically. So after reloading you should see new created Contact. If not, this job will be queued and you can check this in Breezz → On the navigation panel select Breezz Async Jobs
After 2 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.