Attach Actions to Asynchronous Apex Jobs Using Transaction Finalizers (Generally Available)

With Summer ’21, the Transaction Finalizers feature is generally available.

Where: This change applies to Lightning Experience and Salesforce Classic.

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. Valid values for the enum are SUCCESS, and 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, you must implement the Finalizer interface. Only one finalizer instance can be attached to any Queueable job. You can enqueue a single asynchronous Apex job (Queueable, future, or batch) in the finalizer’s implementation of the execute method. Callouts are allowed in finalizer implementations.

For more information on implementing Transaction Finalizers, including examples, see Transaction Finalizers in Apex Developer Guide.