In this blog, we are going to discuss Queueable Apex in Salesforce with example. Queueable Apex is another asynchronous type of apex. It runs in a separate thread and executes whenever the resources are available.
In Queueable Apex 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.
How to use Queueable Apex?
Firstly, to execute queueable apex we need to implement the interface Queueable. Also, the method which is implementing the Queueable interface needs to be declared as public or global.
If you want to allow callout then we also need to implement Database.AllowsCallouts.
Queueable apex only has one method that is execute which takes a QueueableContext as a parameter.
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.
We can monitor the Job in Apex Job in Quick Find Box using the Job Id that we are getting.
Let’s understand Queueable Apex in Salesforce with an example:
Here, we are updating the account Name with “Updated with queueable” having created date of today.
In the above code snippet, we are updating the account name by adding Updated with queueable after the name in our execute method. As we can see currently we have 4 account records which are matching the filter criteria.
Now, let’s enqueue our job
Also Read: Batch Apex in Salesforce with examples
In the above snippet, we first fetch the account with the matching criteria then store the Ids in accountIds and pass it to the constructor of our Queueable Apex.
As we can see the status updated to completed in Apex Job.
There is also another way of monitoring our apex job by querying the AsyncApexJob on our returned jobId.
Example : [Select Id,ApexClass.Name,Status from AsyncApexJob WHERE Id =: JobId];
Updated accounts are
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. This approach is beneficial if there is any dependency on the jobs.
Let’s understand it with an example. In the above example, we updated the account with Queueable Apex. Now we want to update their related contact field (Name).
Above is our new Queueable class that we will chain in our old class QueueableDemoClass.
In the above snippet, we are chaining the UpdateContactNameQueueable in the execute method to update the account’s related contact field Name with the Updated Description.
We have 4 contacts related to the accounts as you can see in the below snapshot.
As we can see our new class is also queued and the status is also completed
After enqueueing it contact name will be updated
Advantages of using Queueable Apex
- Governor limits are higher than synchronous processes as they run in their own thread-like heap size.
- It provides more flexibility like we can pass sObject datatypes.
- We can chain queueable jobs.
- We can implement a Database.AllowCallouts which enables making callouts to external services
Limitations of Queueable Apex
- When chaining jobs with System.enqueueJob, you can add only one job from an executing job. In other words, multiple chaining is not supported.
- Only 50 jobs can be queued with System.enqueue in one single transaction.
Batch vs Queueable Apex
Let’s explore how queueable and batch are different from each other.
- Queueable is faster than Batch Apex as it doesn’t have a start-and-finish method.
- Batch is used to process large-scale data but queueable can be used to process smaller scale data and for chaining.
- In batch, chaining is not supported hence we can schedule jobs in the finish method whereas Queueable supports chaining.
1. What is heap size in Salesforce?
Heap size in Salesforce refers to the amount of memory that is being used by variables, objects and other runtime overhead.
We have different heap sizes for synchronous and asynchronous
- Synchronous – 6 MB
- Asynchronous – 12 MB
2. Can we schedule a Queueable Class?
Yes, we can schedule queueable classes by using the schedulable interface after the queueable interface
In this blog, we discussed the Queueable Apex in Salesforce with example. It is great for processing smaller data sets and for faster execution. It provides more flexibility.