Salesforce Queueable Interface Chaining LIMIT Error

Since you are calling the first queable from a trigger and the limit shows

Number of queueable jobs added to the queue: 1 out of 1

I believe what is happening is

  • You save a record and the trigger runs.
  • It enqueues your first job.
  • The first job runs.
  • The //do stuff (At line 1 in code below) performs a DML or a workflow update is triggered which causes the same trigger to fire again.
  • This trigger again enqueues a job.
  • Now the execution returns to line 3 which again tries to enqueue and fails.
public without sharing class UserProcess implements Queueable{
    public UserProcess () {
    }
    public void execute(QueueableContext context) {
        1 //Do stuff here
        2 //Chain Another Queueable Interface
        3 System.enqueueJob(new RankPermissionProcess(userRank));
     }
}

Quoted from Release notes: Queueable

Queueable Apex Limits

The execution of a queued job counts once against the shared limit for asynchronous Apex method executions.

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

The maximum stack depth for chained jobs is two, which means that you can link a job only once and have a maximum of two jobs in the chain.

When chaining jobs, you can add only one job from an executing job with System.enqueueJob.

You can use the Limits.getQueueableJobs() to check whether a job is already added.

Also you can debug a line before enqueuing job in a trigger and check how many times debug line appeared in the Log


I tried the below example and it works fine:

QueueableExampleOne

public class QueueableExampleOne implements Queueable{

    public void execute(QueueableContext context){
        System.debug(System.LoggingLevel.ERROR,'From queueable example one');
        System.enqueueJob(new QueuableExampleTwo());
    }

}

QueuableExampleTwo

public class QueuableExampleTwo implements Queueable{

    public void execute(QueueableContext ctxt){
        System.debug(System.LoggingLevel.ERROR,'From QueueableContext example two');
    }

}

I started the QueuebleExampleOne Job from developer console using the below code:

System.enqueueJob(new QueueableExampleOne());

And it worked fine and both the Queueable jobs execute methods executed successfully.

I am sure you might have read the below points from the documentation but posting here for the sake of complete answer.

  • No limit is enforced on the depth of chained jobs, which means that you can chain one job to another job and repeat this process with
    each new child job to link it to a new child job. For Developer
    Edition and Trial organizations, the maximum stack depth for chained
    jobs is 5, which means that you can chain jobs four times and the
    maximum number of jobs in the chain is 5, including the initial
    parent queueable job.
  • When chaining jobs, you can add only one job from an executing job with System.enqueueJob, which means that only one child job can exist for each parent queueable job. Starting multiple child jobs from the
    same queueable job isn’t supported.