Sub topic 11
Reading the result with FinalizerContext
This chapter assumes you can write a Queueable class, attach a finalizer with System.attachFinalizer, and read basic governor limits. Here you learn what a finalizer can find out about the parent job, and how to branch on it.
What the finalizer receives
A finalizer implements one method, execute(FinalizerContext ctx). Salesforce runs it in its own transaction after the parent queueable finishes, whether the parent succeeded or failed. The ctx argument describes how the parent ended.
| Method | Returns | Meaning |
|---|---|---|
| `getAsyncApexJobId()` | `Id` | The ID of the parent job, which you can look up in `AsyncApexJob`. |
| `getRequestId()` | `String` | The request ID of the parent job's transaction, useful in logs. |
| `getResult()` | `ParentJobResult` | `SUCCESS` or `UNHANDLED_EXCEPTION`. |
| `getException()` | `Exception` | The unhandled exception, or `null` if the parent succeeded. |
A finalizer that reports the outcome
The finalizer below records the parent job ID, the request ID, and the result. On failure it also records the exception's type and message.
public class ReportingFinalizer implements Finalizer {
public void execute(FinalizerContext ctx) {
String jobId = String.valueOf(ctx.getAsyncApexJobId());
String requestId = ctx.getRequestId();
if (ctx.getResult() == ParentJobResult.SUCCESS) {
System.debug('Job ' + jobId + ' succeeded. Request: ' + requestId);
} else {
Exception ex = ctx.getException();
System.debug(LoggingLevel.ERROR, 'Job ' + jobId + ' failed. Request: '
+ requestId + '. ' + ex.getTypeName() + ': ' + ex.getMessage());
}
}
}The parent attaches it as the first statement in execute. That way the finalizer is registered before any work can throw.
public class ReportedJob implements Queueable {
public void execute(QueueableContext context) {
System.attachFinalizer(new ReportingFinalizer());
List<Account> accounts = [SELECT Id FROM Account LIMIT 10];
System.debug('Loaded ' + accounts.size() + ' accounts');
}
}Common errors
- Reading the exception on success.
getException()returnsnullwhen the parent succeeded. Calling.getMessage()on it then throws aNullPointerException. CheckgetResult()first. - Treating the finalizer as part of the parent transaction. The finalizer has its own governor limits. It cannot roll back or undo the parent's DML, because the parent's transaction has already ended.
- Attaching late. If the parent throws before it reaches
System.attachFinalizer, no finalizer exists and nothing reports the failure. - Comparing the result to a string.
getResult()returns theParentJobResultenum. Compare it withParentJobResult.SUCCESS, not with'SUCCESS'.
Using the context in the next chapter
getResult() and getException() give you the decision point for a retry. Retry logic also needs a count of attempts, which the finalizer carries in its own member variables. The next chapters combine the two.
Exercise
A finalizer's execute method contains System.debug(ctx.getException().getMessage()); with no other checks. The parent job succeeds. What happens, and how do you fix it?
Show answer
getException() returns null when the parent succeeds, so the call throws a NullPointerException inside the finalizer. Fix it by checking ctx.getResult() == ParentJobResult.UNHANDLED_EXCEPTION first, and read the exception only in that branch.