Sub topic 6
Chaining one job from another
This chapter assumes you can write a Queueable class, enqueue it with System.enqueueJob, and read basic governor limits. Chaining means one queueable job enqueues another from inside its own execute method. Each job in the chain runs in its own transaction with fresh limits.
Why chain
A single job has a finite budget of SOQL queries, DML rows, and CPU time. A chain lets you split work across several transactions. It also lets you run steps in order: the second job starts only after the first one has finished. Typical uses are processing a large data set slice by slice, or running step B only after step A has committed its data.
The basic pattern
The parent job creates the child inside execute and enqueues it. The child is a normal queueable. It receives whatever state the parent passes through its constructor.
public class StepOneJob implements Queueable {
private List<Id> accountIds;
public StepOneJob(List<Id> accountIds) {
this.accountIds = accountIds;
}
public void execute(QueueableContext context) {
List<Account> accounts = [SELECT Id, Rating FROM Account WHERE Id IN :accountIds];
for (Account a : accounts) {
a.Rating = 'Warm';
}
update accounts;
// Enqueue the next step only after this step's work is done.
if (!Test.isRunningTest()) {
System.enqueueJob(new StepTwoJob(accountIds));
}
}
}
public class StepTwoJob implements Queueable {
private List<Id> accountIds;
public StepTwoJob(List<Id> accountIds) {
this.accountIds = accountIds;
}
public void execute(QueueableContext context) {
List<Account> accounts = [SELECT Id, Description FROM Account WHERE Id IN :accountIds];
for (Account a : accounts) {
a.Description = 'Step two complete';
}
update accounts;
}
}The Test.isRunningTest() guard is explained below, in the section on tests.
What happens at runtime
The child does not start inside the parent's transaction. It is placed in the queue and starts later, so there can be a delay between the two jobs.
Rules to remember
- A job can enqueue at most one child job from its
executemethod. A secondSystem.enqueueJobcall in the sameexecutethrows aLimitException. - The child sees only the data the parent committed. If the parent's transaction rolls back because of an unhandled exception, the child it enqueued is rolled back too and never runs.
- A parent can pass state to the child only through the child's constructor. There is no shared memory between jobs, so pass IDs or small values and re-query records in the child.
- Chains run in the order you enqueue them, but the platform does not promise a start time.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Too many queueable jobs added to the queue: 2` | Two `enqueueJob` calls in one `execute` | Enqueue one child, and let that child enqueue the next |
| Chain never ends | The child always enqueues another job | Add a stop condition, such as an empty list or an attempt counter |
| Test fails on chained enqueue | Chained jobs cannot run in a test | Skip the chained enqueue when `Test.isRunningTest()` is true |
| Child sees stale data | The child relies on records held in memory | Re-query by ID in the child |
Chains in tests
In a test, Test.stopTest() runs the first queueable only. If that job tries to enqueue a child, Apex throws an error because tests do not allow chained jobs. The Test.isRunningTest() guard in the example avoids the error. Test the child class on its own by enqueuing it directly.
Exercise
A queueable's execute method calls System.enqueueJob twice, once for job B and once for job C. What happens, and how would you run both B and C?
Show answer
The second call throws a LimitException, because a job can enqueue only one child. To run both, enqueue B from the parent and have B enqueue C when it finishes. That gives the order parent, B, C.