Sub topic 7
Limits that apply to enqueuing and chaining
This chapter assumes you can write a Queueable class and call System.enqueueJob. It covers the limits that decide how many jobs you can start and how deep a chain can go. Most chaining errors come from misreading these limits.
Two contexts, two limits
The limit on System.enqueueJob calls depends on where the call runs.
| Where you call `System.enqueueJob` | Jobs allowed per transaction |
|---|---|
| Synchronous code (trigger, controller, anonymous Apex) | 50 |
| Inside a running queueable's `execute` | 1 |
| Inside a finalizer's `execute` | 1 |
The first row is a per-transaction cap. A trigger that enqueues one job per record will fail once a bulk load pushes it past 50.
The second row is why chains are linear. A queueable can start one child, and that child can start one child of its own. A queueable cannot fan out into several children.
Exceeding either cap throws a System.LimitException. You cannot catch a LimitException, so check the limit before you enqueue.
Checking capacity before you enqueue
The Limits class reports how many jobs this transaction has already queued and how many it may queue.
public class SafeEnqueuer {
public static Id enqueueIfAllowed(Queueable job) {
Integer used = Limits.getQueueableJobs();
Integer max = Limits.getLimitQueueableJobs();
if (used >= max) {
System.debug(LoggingLevel.WARN,
'Queueable limit reached: ' + used + ' of ' + max);
return null;
}
return System.enqueueJob(job);
}
}Inside a queueable, Limits.getLimitQueueableJobs() returns 1. The same helper therefore works in both contexts. Callers must handle a null return and decide whether to retry later.
Chain depth
The stack depth is the number of jobs in a chain, counting the first job as depth 1.
- In Developer Edition and trial orgs, the maximum depth is 5. A fifth job cannot enqueue a sixth.
- In other orgs, the depth has no fixed limit. Each job in the chain still gets its own fresh transaction limits.
An unbounded chain is a risk, not a feature. A bug that always re-enqueues will keep running until it uses up your daily asynchronous capacity. Always give a chain a clear end condition, such as an empty list of remaining work.
Other limits that still apply
- Each queueable runs in its own transaction with asynchronous governor limits. These are higher than the synchronous ones: 60,000 ms CPU time and 12 MB heap.
- Enqueued jobs count toward the org's daily asynchronous Apex executions, which is shared with batch, future, and scheduled jobs.
- In a test, a queueable that enqueues a child throws an error unless you guard the call. The chunking chapter used
Test.isRunningTest()for this reason.
Common mistakes
- Enqueuing a job inside a loop over trigger records. Collect the IDs and enqueue one job after the loop.
- Enqueuing two children from one
execute. The second call fails with aLimitException. - Chaining with no end condition, so the job never stops.
Exercise
A trigger processes 200 records and enqueues one queueable per record. What happens, and how do you fix it?
Show answer
The trigger passes 50 enqueues and then throws an uncatchable LimitException on the 51st, which rolls back the transaction. Fix it by collecting the record IDs in a list and calling System.enqueueJob once with a single queueable that takes the whole list, chaining further slices if needed.