Queueable Apex Chaining with Example

Queueable Apex Chaining with Example

This blog will delve into the intricacies of Queueable Apex Chaining Example. Queueable Apex is one of the processes used in Asynchronous Apex. It provides more flexibility over the future. It helps us to monitor our jobs.

Also, the most powerful technique in Queueable Apex is chaining, where one job initiates another, creating a series of interdependent tasks.

So, let’s first understand what queueable is and how it is executed.

What is the Queueable Apex?

Queueable Apex is a robust asynchronous feature that allows developers to submit jobs for execution by the Apex job queue, effectively handling these challenges. In this type, we can queue our jobs and it is used to run long-running operations. Here we can get the job ID to monitor our jobs.

What is the syntax?

How to Execute Queueable Apex?

To execute queueable apex we have to use System.enqueueJob. We can execute from the Execute Anonymous window in the Developer Console.

Syntax :

What is chaining in Queueable Apex?

Chaining in Queueable means that one job can enqueue another job to run after it completes. Chaining allows you to break down complex tasks into smaller, more manageable units of work and execute them sequentially.

However, this approach is beneficial if there is any dependency on the jobs.

Let’s understand the Queueable Apex Chaining with Example

Scenario – We want to process a list of accounts with Account Status as “Inactive” to update their contact and then send notification emails. So, here we know it has multiple logic to develop and building all these in one single logic would lead to a complex and unmanageable code.

Hence, we will break this into different apex classes to maintain its readability and more manageable.

As per the scenario, we want to achieve three different functionality. So each step will be represented by a separate Queueable class, and we will chain them together to achieve the desired workflow.

  1. Processing list of Account
  2. Updating the associate contacts
  3. Send Notification emails

Processing list of Accounts

In this step, we will create a Queueable class to fetch and process a list of accounts. Once processed, this class will chain to the next job to update contacts.

Syntax

Here we have one inactive account

Queueable chaining example

Updating the associate contacts

In this step, we will create a Queueable class to update the contacts associated with the accounts processed in the previous step.

Syntax

Here we have 2 contacts for the above account

two contact found

Send Notification emails

In this final step, we will create a Queueable class to send notification emails to the contacts updated in the previous step.

Syntax

Putting all logic together

We can enqueue the first job from an Apex trigger, a button click, or a scheduled job to initiate the chain of Queueable jobs. So, here’s an example of how you might start the chain. As of now we simply enqueue it from the developer console.

Syntax

OUTPUT

We have 2 contacts so email is triggered to both the contacts.

output

 

Also, the description is updated.
output 2

 

FAQ’s

1. How many Queueable jobs can I chain together?

You can chain up to 50 Queueable jobs in a single transaction. Hence, if you exceed this limit, you’ll encounter a System.LimitException.

2. How to perform callout in Queueable Apex?

By implementing a Database.AllowsCallouts after queueable interface, our apex will be available for doing callouts.

3. How does Queueable Apex compare to Batch Apex for handling large datasets?

While Queueable Apex is powerful for chaining and handling complex logic, Batch Apex is generally more suitable for processing large datasets due to its built-in chunking mechanism. If you have a job that needs to process millions of records, Batch Apex might be more appropriate.

Conclusion

In conclusion, Queueable Apex chaining is a powerful pattern in Salesforce development that allows for the sequential execution of complex, interdependent tasks. By breaking down processes into smaller, manageable Queueable jobs, developers can enhance code maintainability, scalability, and performance.

Moreover, the example provided demonstrates a practical implementation of Queueable Apex chaining to process accounts, update associated contacts, and send notification emails. This approach can be easily adapted to a variety of scenarios, making it a valuable tool in your Salesforce development toolkit.

Get a complete Roadmap To Learn Salesforce Admin And Development

Share Now

Kashish have extensive Salesforce development experience, holding 4 Salesforce certifications. she posses expertise in Apex, Lightning Web Components, and Salesforce Admin, with a track record of successful project delivery. As a dedicated Salesforce enthusiast, she actively seek and embrace new challenges and opportunities within the dynamic Salesforce ecosystem.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *