Attach Actions to Asynchronous Apex Jobs Using Transaction Finalizers (Beta)

With Spring ’21, the Transaction Finalizers feature is in beta. A new limit establishes that a Queueable job that failed due to an unhandled exception can only be successively re-enqueued five times by a Transaction Finalizer. This limit applies to a series of consecutive Queueable job failures. The counter is reset when the Queueable job completes without an unhandled exception. Finalizers can be implemented as an inner class. Also, you can implement both Queueable and Finalizer interfaces with the same class.

Where: This change applies to Lightning Experience and Salesforce Classic. To use this beta feature, it isn’t necessary to enable the 'Transaction Finalizers' feature in scratch orgs. The Transaction Finalizers feature isn’t restricted to scratch orgs, can be used in production orgs and sandboxes, and can be included in managed packages.

Note

Note

As a beta feature, Transaction Finalizers is a preview and isn’t part of the “Services” under your master subscription agreement with Salesforce. Use this feature at your sole discretion, and make your purchase decisions only on the basis of generally available products and features. Salesforce doesn’t guarantee general availability of this feature within any particular time frame or at all, and we can discontinue it at any time. This feature is for evaluation purposes only, not for production use. It’s offered as is and isn’t supported, and Salesforce has no liability for any harm or damage arising out of or in connection with it. All restrictions, Salesforce reservation of rights, obligations concerning the Services, and terms for related Non-Salesforce Applications and Content apply equally to your use of this feature. You can provide feedback and suggestions for this feature in the TransactionFinalizers group in the Trailblazer Community.

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 FinalizerContext 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 (Beta) in Apex Developer Guide.