Salesforce Asynchronous Apex: How to chain Queueable Apex Job

 

Looking for Salesforce Training & HandsOn Projects?


Queueable Apex is just another flavor of Async Programming implementation offered by the Salesforce Platform. 

You can refer to my earlier post on Queueable Job to start fresh with the concept:

Salesforce Asynchronous Apex: How to implement Queueable Apex Job

In this article, we will explore how to chain one Queueable Job from another to accomplish a series of tasks in a predictable manner. 

We can start with a simple demo to explain the concept in the following steps:  

Step-1: The source of data for this demo will “Accounts” Object as shown below


Step-2: We need to build the “SOQL” statement that we can use to query the data from Queueable Job. We can quickly build & test the SOQL using “Workbench”. This step will make sure that we don’t run into issues later at least for a defective SOQL Query. Write down the query as you need. 

Step-3: Execute the query & test the results to make sure that this is the intended result set to be operated by the Queueable Job



Step-4:  Create a brand new Apex Class



Step-5: Give it a suitable name



Step-6: Create a Queueable Job (“Parent Job”). If you need more details about the constructs used to create the Queueable Job, please refer to my previous post on this topic. 

This will be the Parent Job that will be updating the “Account” Records


Step-7: Create another brand new Apex Class



Step-8:  Give it a suitable name



Step-9:  Create a Queueable Job (“Child Job”). This will be the Child Job that will be updating the “Contacts” Records



Step-10: Now go back to “Parent Job” & add another “System.enqueueJob” as the last statement in the “execute” method of the Parent Job


Step-11: Now to test this Chaining Process, launch “Execute Anonymous Window” & queue the Parent Job for processing



Step-12 & 13: We can check for debug logs to review the runtime information for this job related to its actions & outcomes






Step-14: If Parent Job executes successfully then we should be able to see the records updated in the Accounts Object as shown below



Step-15: Also if Child Job executes successfully then we should be able to see the records updated in the Contacts Object as shown below



There is more to this story, we will be having more articles regarding Monitory & Other Design Patterns that can be used to manage the job efficiently. So stay tuned.

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