Salesforce Asynchronous Apex: How to chain Batch Apex Jobs

Looking for Salesforce Training & HandsOn Projects?

Trailblazer Profile | LinkedIn | Salesforce Blog | Facebook | Youtube Channel | WhatsApp Community


Salesforce offers Batch Apex implementation that supports the processing of a humungous volume of data (millions of records) asynchronously in the form of batches without violating the platform governance limits.

 

To know how to start with Batch Class development, you can refer to the previous article Salesforce Asynchronous Apex: How to work with Batch Apex Class

 

In this article, we will look into the design pattern enabling Batch Job Chaining to execute a series of complex processes.

 

To start with a demo we need to first analyze the target Salesforce Object that will be impacted.

 

Step-1: Launch the Salesforce Object & navigate to the Cases Object. Here we can see that a few records are already created & and at the end of the execution of this Job we will be having a few more records added as part of this execution.



Step-2: Since we would be needing a valid SOQL to pull the records from Cases Object on this the Job will operate. We can make use of Workbench to prepare a valid SOQL Statement

 

Step-3: Once the SOQL is compiled, we can click on the “Query” button to test the query & find out how many records are being returned by this query



Step-4: Since we are talking about chaining the Batch Job, so we need another SObject to operate on. For this demo I am going to use Contacts Object as shown below-



Step-5 & 6:  As explained in Step-2&3 we are now preparing a valid SOQL on Contacts sObject for the chained job to operate



Step-7: We add an Apex Class & implement the Batchable Interface to start with the Batch Job

 

Step-8 & 9: We will make use of the “Database.getQueryLocator” function to query the records from the sObject and as we know this is useful to avoid usual SOQL Limits.

 

Step-10, 11 & 12: In the “execute” function we are going to update the Case Status as “Working” & update the records using a bulk update pattern. This pattern is strongly recommended to adopt as a thumb rule while working with Salesforce Records (DML Operations)

 

Step-13 & 14: In the finish function, we are printing a success message to the debug logs and triggering another Batch Job to update the Contact Records. Notice the Batch Size= 5 is specified in the signature



Step-15 to 21: In these steps, we are updating Contact’s First Name & updating the records using Bulk Pattern. Then finally in the finish method, we are writing the success message to the debug log.


Step-22:  Now it is time to execute the first job, which will execute the chained job once done. Launch “Execute Anonymous Window” to execute the Job



Step-23: In this step, we can see the code to execute the Batch Job. Notice how we specify the Batch Size based on the anticipated volume of data. “BatchUpdateJob” will execute first & then will trigger the “UpdateContacts” Job (Chained Operation)



Step-24: As a result of the Job execution, we can see Case Status is now updated as per the logic defined in the “BatchUpdateJob” Job



Step-25: As a result of the Job execution, we can see Contact’s First Name is now updated as per the logic defined in the “UpdateContacts” Job



Step-26 & 27: We can optionally validate the records using SOQL statements for Case & Contact sObject in Workbench or any other IDE of your choice


Hope you enjoyed this article. Please leave your comments to let me know how you do like the content and how you do find it helpful to learn the topic.


Comments