Attach Actions to Asynchronous Apex Jobs Using Transaction Finalizers (Pilot)
With Summer ’20, we enhance the Transaction Finalizers feature
with a FinalizerContext.getRequestId() method,
which returns the request ID of the Finalizer execution.
Where: This change applies to Lightning Experience and Salesforce Classic only in scratch orgs that have enabled the feature during org creation. Because finalizers are currently in pilot and are available only in scratch orgs, do not attempt to package finalizers.
How:
The System.FinalizerContext interface contains
four methods.
- getAsyncApexJobId method: Returns the ID of the queueable job for which this finalizer is defined.
- getRequestId method: Returns the request ID shared by both the finalizer execution and the Queueable job to which the finalizer is attached. This shared ID helps in filtering logs of a Queueable job and its attached finalizer.
- getResult method: Returns the System.ParentJobResult enum, which represents the result of the parent asynchronous Apex queueable job to which the finalizer is attached. The enum takes these values: SUCCESS, UNHANDLED_EXCEPTION.
- getException method: Returns the exception with which the queueable job failed when getResult is UNHANDLED_EXCEPTION, null otherwise.
To attach actions to your queueable jobs, implement the FinalizerContext interface as
follows.
- Define a class that implements the System.FinalizerContext interface.
- Attach a finalizer within a queueable job’s execute method. To attach the finalizer, invoke the System.attachFinalizer method, using as argument the instantiated class that implements the System.FinalizerContext interface.
For more information on Transaction Finalizers, including examples, see Transaction Finalizers (Pilot) in Apex Developer Guide.

