Sub topic 3
Carrying state in member variables
This chapter assumes you can write basic Apex classes, know how governor limits apply per transaction, and have enqueued a simple Queueable. Here you will see how a job gets its input and what happens to that input between enqueue and run.
How state travels
A Queueable job runs in a different transaction from the code that enqueued it. The job can't read the caller's local variables. Its only inputs are the member variables of the object you pass to System.enqueueJob.
When you call System.enqueueJob, Apex serializes that object, including its member variables, and stores it. When the platform runs the job, it rebuilds the object and calls execute. The constructor does not run again at that point.
A snapshot, not a reference
The job receives the values as they were at the moment of enqueue. If the caller changes its own variables afterwards, the job does not see the change.
List<Id> ids = new List<Id>{ someAccountId };
System.enqueueJob(new AccountRatingJob(ids));
ids.clear(); // the queued job still holds the original IDThis matters for SObjects too. An Account stored in a member variable holds the field values it had at enqueue time. By the time the job runs, the record may have changed. When freshness matters, store the record IDs and query inside execute. The first example in this tutorial, AccountRatingJob, does exactly this.
Passing state down a chain
Each job in a chain is a new object. To pass state forward, give the next job its values through its constructor. This job counts how many accounts it has handled across the whole chain:
public class CountingJob implements Queueable {
private List<Id> remaining;
private Integer processedSoFar;
public CountingJob(List<Id> remaining, Integer processedSoFar) {
this.remaining = remaining;
this.processedSoFar = processedSoFar;
}
public void execute(QueueableContext context) {
Id current = remaining.remove(0);
Integer total = processedSoFar + 1;
System.debug('Handled ' + current + ', total so far: ' + total);
if (!remaining.isEmpty() && !Test.isRunningTest()) {
System.enqueueJob(new CountingJob(remaining, total));
}
}
}Start it with System.enqueueJob(new CountingJob(accountIds, 0));.
What does not carry over
- Static variables. Every job starts a fresh transaction, so statics reset to their initial values. Pass the value through a member variable instead.
- Transient members. A member marked
transientis skipped during serialization and isnullinexecute. Use it only for values you can rebuild insideexecute. - Non-serializable types. Member variables must be types Apex can serialize. Primitives, SObjects, collections of those, and Apex classes built from them are fine. Other types can fail at enqueue or arrive empty.
Keep the state small
The serialized state is loaded into the heap of the job transaction, so a large state uses heap space before your code does any work. A list of 50,000 IDs is much cheaper than a list of 50,000 full SObjects with many fields. Prefer IDs, small values, and counters.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Storing an SObject and assuming it is current | Job works on stale field values | Store the ID and query in `execute` |
| Using a static variable to pass input | Value is the default at run time | Use a member variable set in the constructor |
| Marking a needed member `transient` | Member is `null` in `execute` | Remove `transient` or rebuild the value |
| Passing a huge list of full records | Heap pressure in the job | Pass IDs only |
Exercise
A trigger enqueues a job with new ReportJob(acct), where acct is an Account SObject held in a member variable. Ten minutes later the job runs and writes the Account's old Name into a report. Why, and what is the fix?
Show answer
The job received a snapshot of the Account taken at enqueue time, so it holds the old field values even if the record has since changed. Store the Account's ID in the member variable and query the current Name inside execute.