Batch Apex classes can opt in to fire platform events when encountering an error or
exception. Clients listening on an event can obtain actionable information, such as how often
the event failed and which records were in scope at the time of failure. Events are also fired
for Salesforce Platform internal errors and other uncatchable Apex exceptions such as
LimitExceptions, which are caused by reaching governor limits.
As a beta feature, Batch Apex Error Events 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.
An event record provides more granular error tracking than the Apex Jobs UI. It includes
the record IDs being processed, exception type, exception message, and stack trace. You
can also incorporate custom handling and retry logic for failures. You can invoke custom
Apex logic from any trigger on this type of event, so Apex developers can build
functionality like custom logging or automated retry handling.
For information on subscribing to platform events, see Subscribing to Platform Events.
The BatchApexErrorEvent object represents a platform event associated with a batch Apex
class. This object is available in API version 44.0 and later. For more details, see
BatchApexErrorEvent (Beta).
To fire a platform event, a batch Apex class declaration must implement the
Database.RaisesPlatformEvents interface.
Example
This example creates a trigger to determine which accounts failed in the batch
transaction. Custom field Dirty__c indicates that the account was one of a failing batch
and ExceptionType__c indicates the exception that was encountered. JobScope and
ExceptionType are fields in the BatchApexErrorEvent
object.