Salesforce Asynchronous Apex: Batch Apex Job Chaining Limitations

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 articles-

Salesforce Asynchronous Apex: How to work with Batch Apex Class

Salesforce Asynchronous Apex: How to chain Batch Apex Jobs

 

In this article, we will review the know limitations of Batch Apex Chaining & understand the recommended implementation of the chaining pattern.

 

Step-1: To start with let’s review the Accounts Object which is going to be the source of records for this demo



Step-2 & 3: As we did in earlier articles, we will use Workbench & prepare the SOQL Statement to be used in Batch Apex to provide actionable records. We will test the SOQL & ensure that it returns the desired records only


Step-4: We also need to review Opportunity Object which will be used as another source of record for another Batch Apex (To be chained). Here we are focusing only on Opportunities created for Account = “Edge Communication”. We can filter the view using the View Filter feature as shown below-


Step-5: After the filter is applied the Opportunity List View would look something like as shown below-


Step-6 & 7: As we did in earlier steps, we will use Workbench & prepare the SOQL Statement to be used in Batch Apex to provide actionable records. We will test the SOQL & ensure that it returns the desired records only


Step-8 to 13: We are creating a Batch Apex class “UpdateAccounts” as usual. If you need a detailed explanation on this please refer to the earlier articles on Batch Apex.

The only catch in this Batch class is on Step-12, where we are trying to chain another Batch Apex class inside the “execute” method instead of the “finish” method. 

We will execute this code & see the outcome of this execution.


Step-14 to 20: Now we are creating the “UpdateOpportunities” Batch class that we are chaining in the previous “UpdateAccounts” Batch class


Step-21: Now we are all set with both Parent & Child Batch classes. It is time to kick off the Batch Job & review the Outcomes.

Launch the “Execute Anonymous” window & execute the Parent Batch Job

Step-22: Now we can review the debug logs to analyze the outcomes.

As shown below, Parent Job executes & updates the Account Records successfully. Then Parent Job tries to execute the Child Job from inside the “execute” method & we can see this operation got failed.


Step-23: If we review the details of the error generated due to this operation failure we can see an Async Exception saying that Batch Class chaining is not allowed from inside of “start”, “execute” methods & “future” methods


As a developer, we must follow the allowed design patterns with Batch Apex Job & plan solutions accordingly.

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

Comments