html code

Salesforce Queueable Apex

Queueable Apex in Salesforce represents a more sophisticated iteration of future methods, encompassing additional functionalities. Combining the ease of future methods with the capabilities of Batch Apex, Queueable Apex provides a unified structure. It introduces a serialized class structure on the platform, offering a streamlined interface sans the need for start and finish methods. Moreover, it extends beyond primitive arguments, allowing a broader range of data utilization. Invocation involves a straightforward System.enqueueJob() method, generating a job ID for monitoring purposes. To execute queueable operations, implementing the Queueable interface is essential.

Benefits of utilizing Queueable Apex

Queueable jobs, akin to future methods, are queued for execution, yet they offer distinct advantages over future methods:

  • Upon submitting our job through the System.enqueueJob method, we receive the ID of the newly created job. This ID aligns with the AsyncApexJob record’s ID, enabling us to track and monitor its progress. Monitoring can be done via the Salesforce UI on the Apex Jobs page or programmatically by querying the AsyncApexJob record using this ID.
  • Utilizing non-primitive types: Your queueable class is capable of incorporating member variables comprising non-primitive data types, such as sObjects or custom Apex types. These objects remain accessible during job execution.
  • Job chaining involves initiating a second job from a currently running job, creating a sequence of interconnected jobs. This method proves beneficial when a subsequent process relies on the completion of an earlier process for its execution.

Queueable versus Future

Given that queueable methods offer functionality similar to future methods, in most cases, you’d likely prefer using queueable methods over future methods. However, this doesn’t imply an immediate need to refactor all your existing future methods. If your future method was hitting a governor limit or you anticipate a need for higher limits in a future method, there’s a possibility to increase these limits¬†Future Methods with Higher Limits¬†pilot..

Opting for future methods over queueable methods is advantageous when your functionality is occasionally executed synchronously and at other times asynchronously. Refactoring a method in this way proves simpler compared to converting it into a queueable class. This approach becomes useful when you realize that a portion of your current code requires transitioning to asynchronous execution.

Syntax of Queueable Apex

To comply, create an Apex Class incorporating the Queueable Interface, housing a single method named execute. Additionally, implement Database.AllowsCallouts if callout processing from the Queueable apex is required.

public class SomeClassName implements Queueable { 
    public void execute(QueueableContext context) {
        // awesome code here

Run Queueable Apex

Invoke the Queueable apex using the System.enqueueJob method, which will provide the job ID. Upon enqueuing a new job, you receive a job ID that you can monitor either through the Apex Jobs section or by utilizing the AsyncApexJob object.

ID jobID = System.enqueueJob(new SomeClassName());

Example of Queueable Apex

In this instance, we’ll add ‘sfdcpoint’ to the conclusion of the account name.

public class AccountQueueableExample implements Queueable {
    public List<Account> accList ; 
    public AccountQueueableExample(List<Account> accList){
        this.accList = accList ;  
    public void execute(QueueableContext context) {
        for(Account acc :accList){
            // Update the Account Name 
            acc.Name = acc.Name + 'sfdcpoint';
        update accList;

Execute the job using the following code in the execute anonymous window.

List<Account> accList = [Select Id , Name from Account ];
ID jobID = System.enqueueJob(new AccountQueueableExample(accList));

Test class designed for Queueable Apex

public class AccountQueueableExampleTest {
    static void setup() {
        List<Account> accounts = new List<Account>();
        // add 100 accounts
        for (Integer i = 0; i < 100; i++) {
            accounts.add(new Account(
                name='Test Account'+i
        insert accounts;
    static testmethod void testQueueable() {
        // query for test data to pass to queueable class
        List<Account> accounts = [select id, name from account where name like 'Test Account%'];
        // Create our Queueable instance
        AccountQueueableExample accQObj = new AccountQueueableExample(accounts);
        // startTest/stopTest block to force async processes to run
        // Validate the job ran
        System.assertEquals(100, [select count() from account where Name like = '%sfdcpoint%']);

Chaining of Queueable Jobs

Job chaining stands as one of Queueable Apex’s finest attributes. When a sequence of sequential job runs becomes necessary, Queueable Apex simplifies the process. To chain one job to another, submit the second job from the execute() method within your queueable class. It’s essential to note that only one job can be added from an executing job, implying that each parent job can have only one child job. For instance, if you have a class named SecondJob that implements the Queueable interface, you can enqueue this class within the execute() method in the following manner:

public class FirstJob implements Queueable { 
    public void execute(QueueableContext context) { 
        // Awesome processing logic here    
        // Chain this job to next job by submitting the next job
        System.enqueueJob(new SecondJob());

Limitations of Queueable Apex

Queueable Apex is an excellent addition, yet there are some considerations to keep in mind:

  • The execution of a queued job counts as a single instance towards the shared limit for asynchronous Apex method executions.
  • Within a single transaction, System.enqueueJob enables the addition of up to 50 jobs to the queue.

While chaining jobs, using System.enqueueJob, you’re restricted to adding just one job from an executing job. Consequently, each parent queueable job can have only one child job. Initiating multiple child jobs from the same queueable job is prohibited.

  • There’s no restriction on the depth of chained jobs, allowing you to link one job to another and continue this pattern with each new child job. However, in Developer Edition and Trial orgs, the maximum stack depth for chained jobs is 5. This implies that you can chain jobs up to four times, resulting in a maximum chain of 5 jobs, inclusive of the initial parent queueable job.

For further information regarding Queueable Apex, please consult the “Control Processes with Queueable Apex” Trailhead module.