Queueable Apex in Salesforce with example

Queueable Apex in Salesforce with example

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.

Syntax:

 

Queueable apex only has one method that is execute which takes a QueueableContext as a parameter.

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 :

 

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:

Scenario

Here, we are updating the account Name with “Updated with queueable” having created date of today.

Syntax

 

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.

 

Account list view

 

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.

Queueable Apex Job tab

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

Output generated

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).

Syntax:

UpdateContactNameQueueable.apxc

 

Above is our new Queueable class that we will chain in our old class QueueableDemoClass.

QueueableDemoClass.apxc

 

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.

Contact List view

 

As we can see our new class is also queued and the status is also completed

Queueable Apex next apex job view

 

After enqueueing it contact name will be updated

Output generated

 

Advantages of using Queueable Apex

  1. Governor limits are higher than synchronous processes as they run in their own thread-like heap size.
  2. It provides more flexibility like we can pass sObject datatypes.
  3. We can chain queueable jobs.
  4. We can implement a Database.AllowCallouts which enables making callouts to external services

Limitations of Queueable Apex

  1. When chaining jobs with System.enqueueJob, you can add only one job from an executing job. In other words, multiple chaining is not supported.
  2. 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.

  1. Queueable is faster than Batch Apex as it doesn’t have a start-and-finish method.
  2. Batch is used to process large-scale data but queueable can be used to process smaller scale data and for chaining.
  3. In batch, chaining is not supported hence we can schedule jobs in the finish method whereas Queueable supports chaining.

FAQ’s

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

Syntax:

 

Conclusion

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.

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

One thought on “Queueable Apex in Salesforce with example

Leave a Reply

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