Sub topic 13
Rules and limits for finalizers
This chapter lists what a transaction finalizer may and may not do. It assumes you can write a Queueable class, attach a finalizer with System.attachFinalizer, and read a FinalizerContext, as covered in the earlier chapters.
What a finalizer is allowed to do
A finalizer runs in its own transaction after the parent job ends. That transaction gets fresh governor limits. It runs whether the parent succeeded, threw an unhandled exception, or hit a limit. This is why a finalizer can still log or re-enqueue after the parent failed.
The rules
| Rule | What it means in practice |
|---|---|
| One finalizer per job | Attaching a second finalizer in the same `execute` throws an exception. Decide which finalizer applies before attaching. |
| Attach inside `execute` | `System.attachFinalizer` works only while a queueable is running. Attach it early, before code that might fail. |
| One enqueue | A finalizer may call `System.enqueueJob` once. A second call fails with a limit error. |
| Separate transaction | The parent's DML is rolled back after an unhandled exception. The finalizer's own DML is committed independently. |
| Not guaranteed | In rare cases, such as a platform failure, the finalizer does not run. Do not rely on it for correctness-critical cleanup. |
Working within the single-enqueue limit
A finalizer often has several possible next steps: retry, alert, or move on. Only one can enqueue a job. Pick the branch first, then enqueue once.
public class SafeFinalizer implements Finalizer {
private static final Integer MAX_ATTEMPTS = 3;
private List<Id> recordIds;
private Integer attempt;
public SafeFinalizer(List<Id> recordIds, Integer attempt) {
this.recordIds = recordIds;
this.attempt = attempt;
}
public void execute(FinalizerContext ctx) {
Queueable next = null;
if (ctx.getResult() != ParentJobResult.SUCCESS && attempt < MAX_ATTEMPTS) {
next = new ImportJob(recordIds, attempt + 1);
}
// Exactly one System.enqueueJob call, outside any loop.
if (next != null) {
System.enqueueJob(next);
}
}
}Never call System.enqueueJob inside a loop in a finalizer. The second iteration fails and the error hides the real work.
Common mistakes
- Attaching twice. This happens when a helper method and
executeboth attach. Attach from one place. - Assuming parent DML survives. After a failure, the parent's changes are gone. A finalizer that expects those rows to exist will see old data.
- Doing heavy work in the finalizer. Keep it short: read the context, log, and enqueue. Move real processing into a new queueable.
- Unbounded retries. A finalizer that always re-enqueues creates an endless loop. Always compare an attempt counter to a maximum, as above.
- Hidden state. The finalizer cannot read the parent's variables. Pass what it needs through its constructor.
Exercise
A finalizer needs to log a failure and then either retry the job or enqueue a notification job, depending on the exception type. How should it be written so it stays within the finalizer rules?
Show answer
Choose the next job with an if/else and store it in one variable, then call System.enqueueJob once. A finalizer can enqueue only one job, so it cannot enqueue both a retry and a notification. If both are needed, enqueue one job that performs the other step, or have the retried job send the notification when it ends.