Sub topic 5
Chaining Queueable jobs for sequential steps
This chapter assumes you can write a basic Queueable class and enqueue it with System.enqueueJob. It also assumes you know that each async job runs in its own transaction.
What chaining means
A running Queueable job can enqueue one more Queueable job from its execute method. The second job is the child. It starts only after the parent's transaction finishes, and it gets a fresh set of governor limits.
Chaining is useful in two cases:
- Steps must run in order, and step 2 needs the result of step 1.
- A list is too large for one transaction, so you process it in pieces.
Several Queueable jobs enqueued separately run in no guaranteed order. A chain is how you get order.
Example: process a list in chunks
This job updates up to 100 accounts, then enqueues a copy of itself with the rest of the list.
public class ChunkedUpdateJob implements Queueable {
private List<Account> remaining;
public ChunkedUpdateJob(List<Account> remaining) {
this.remaining = remaining;
}
public void execute(QueueableContext ctx) {
Integer chunkSize = Math.min(100, remaining.size());
List<Account> chunk = new List<Account>();
for (Integer i = 0; i < chunkSize; i++) {
chunk.add(remaining.remove(0));
}
update chunk;
if (!remaining.isEmpty()) {
System.enqueueJob(new ChunkedUpdateJob(remaining));
}
}
}Three details make this safe:
- The state travels in the constructor. The child receives only what you pass it.
- The job enqueues a child only when work remains. That condition ends the chain.
- The job calls
System.enqueueJobat most once.
Limits that apply to chains
| Situation | Limit |
|---|---|
| Jobs enqueued from a running Queueable's `execute` | 1 |
| Jobs enqueued from a synchronous transaction | 50 |
| Chain depth in Developer Edition and trial orgs | 5 |
| Chain depth in other orgs | No fixed limit |
Chained jobs still count against the org-wide daily async Apex execution limit, so a very long chain uses up that shared allowance.
You can pass a delay in minutes (0 to 10) as a second argument, for example System.enqueueJob(job, 5). The platform then waits before it runs the job. This helps when a chain polls an external system that needs time to finish.
Common errors
- Enqueueing two children. Calling
System.enqueueJobtwice insideexecutethrows aLimitException("Too many queueable jobs added to the queue: 2"). If you need parallel work, enqueue the jobs from a synchronous context or from a Batch job instead. - A chain that never ends. If the stop condition depends on data that your job does not change, the job re-enqueues itself forever. Test the condition with a small list first.
- Chaining in tests. Apex tests do not allow a chained job to run. The child's
enqueueJobcall throws an error about stack depth. Guard it so the test can exercise the first job on its own:
if (!remaining.isEmpty() && !Test.isRunningTest()) {
System.enqueueJob(new ChunkedUpdateJob(remaining));
}- Losing work on failure. If one job throws an unhandled exception, it never enqueues its child, and the chain stops silently. The next chapter covers finalizers, which can enqueue a retry.
When not to chain
If you are chunking a large record set by hand, ask whether Batch Apex fits better. Batch gives you a query locator that can read up to 50 million records, a built-in scope size, and a finish method. Chaining suits a small number of known steps, or work where each step decides what comes next.
Exercise
A Queueable job's execute method needs to start two follow-up Queueable jobs, one for Accounts and one for Contacts. It calls System.enqueueJob twice and fails. Why, and what are two ways to fix it?
Show answer
A running Queueable job can enqueue only one child job, so the second call exceeds the limit. Fix 1: chain the jobs, so the first job enqueues the Accounts job and the Accounts job enqueues the Contacts job when it finishes. Fix 2: enqueue both jobs from a synchronous context, such as a trigger handler or a Batch job, where up to 50 jobs are allowed. Fix 1 gives you guaranteed order, and Fix 2 does not.