Salesforce Asynchronous Apex: How to implement Queueable Apex Job

Looking for Salesforce Training & HandsOn Projects?

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

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

In this article, we will explore the implementation details of Queueable Apex & will deep dive into the core constructs required to build the class. 

Queueable Apex is the “public” class that implements the “Queueable” Interface. This interface contains a method “execute” which the consumer class needs to override to provide the concrete implementation. 

This “execute” method can access the “instance properties” of the class. This method always returns “void”. 

This Queueable Apex can be put into the execution queue by calling “System.enqueueJob (<Queueable Class Instance>)”. This function will return the “AsyncApexJob Id” that can be used to monitor the Job’s progress at a later point in time. 

Queueable Apex can be chained, which means we can execute one Queueable Class from another & this keeps going on the same way. 

Chaining the Job has got the following considerations:

1.    You can add up to “50 jobs” to the queue with “System.enqueueJob” in a “single transaction”.

2.    Since “NO limit” is enforced on the depth of chained jobs, we can chain one job from another & this can go on and on.

3.    When chaining jobs with System.enqueueJob, you can add only “ONE JOB” from an executing job. Only one child job can exist for each parent's Queueable job. Starting multiple child jobs from the same Queueable job isn’t supported. 

With this background, 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 “Leads” 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.

Step-3:  Write down the query as you need. In this demo, I am keeping things simple for the sake of ease in learning core concepts

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



Step-5: Now let’s write some code using “Developer Console”. Go to Settings & Launch Developer Console as shown below


Step-6 & 7: Create a brand new Apex Class



Step-8: Give it a suitable name



Step-9: Implement the “Queueable” Interface to expose this class as a Queueable Job

Step-10: Override the “execute” method. Notice the signature of this method, it takes a context parameter “QueueableContext” initialized by the Platform Runtime to include the Job Context into this class

Step-11: Make use of the SOQL statement to query records to operate on in this Job

Step-12 & 13: Apply the logic you like to implement on these records

Step-14: Don’t ever forget the “Bulk Update Pattern” (Job Saver really). Update the records back to Salesforce as a Bulk Operation


Step-15: Once done with the code, we can try to execute it using the “Execute Anonymous Window”



Step-16:  We can queue this Apex Class as a Job using “System.enqueueJob” by passing the class instance to it as an input parameter



We can check for debug logs to review the runtime information for this job related to its actions & outcomes



Step-17: If Job executes successfully then we should be able to the records updated in the Lead Records as shown below



There is more to this story, we will be having more articles regarding Chaining, 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.

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

Comments