Newer Version Available

This content describes an older version of this product. View Latest

Firing Platform Events from Batch Apex

Batch Apex classes can 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.

An event message 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. If the start, execute, or finish method of a batch Apex job encounters an unhandled exception, a BatchApexErrorEvent platform event is fired. For more details, see BatchApexErrorEvent in the Platform Events Developer Guide.

To fire a platform event, a batch Apex class declaration must implement the Database.RaisesPlatformEvents interface.
1public with sharing class YourSampleBatchJob implements Database.Batchable<SObject>, 
2   Database.RaisesPlatformEvents{ 
3   // class implementation 
4}

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.
1trigger MarkDirtyIfFail on BatchApexErrorEvent (after insert) {
2    Set<Id> asyncApexJobIds = new Set<Id>();
3    for(BatchApexErrorEvent evt:Trigger.new){
4        asyncApexJobIds.add(evt.AsyncApexJobId);
5    }
6    
7    Map<Id,AsyncApexJob> jobs = new Map<Id,AsyncApexJob>(
8        [SELECT id, ApexClass.Name FROM AsyncApexJob WHERE Id IN :asyncApexJobIds]
9    );
10    
11    List<Account> records = new List<Account>();
12    for(BatchApexErrorEvent evt:Trigger.new){
13        //only handle events for the job(s) we care about
14        if(jobs.get(evt.AsyncApexJobId).ApexClass.Name == 'AccountUpdaterJob'){
15            for (String item : evt.JobScope.split(',')) {
16                Account a = new Account(
17                    Id = (Id)item,
18                    ExceptionType__c = evt.ExceptionType,
19                    Dirty__c = true
20                );
21                records.add(a);
22            }
23        }
24    }
25    update records;
26}

Testing BatchApexErrorEvent Messages Published from Batch Apex Jobs

Use the Test.getEventBus().deliver() method to deliver event messages that are published by failed batch Apex jobs. Use the Test.startTest() and Test.stopTest() statement block to execute the batch job.

This snippet shows how to execute a batch Apex job and deliver event messages. It executes the batch job after Test.stopTest(). This batch job publishes a BatchApexErrorEvent message when a failure occurs through the implementation of Database.RaisesPlatformEvents. After Test.stopTest() runs, a separate Test.getEventBus().deliver() statement is added so that it can deliver the BatchApexErrorEvent.

1try {
2    Test.startTest();
3    Database.executeBatch(new SampleBatchApex());
4    Test.stopTest();
5    // Batch Apex job executes here
6} catch(Exception e) {
7    // Catch any exceptions thrown in the batch job
8}
9
10// The batch job fires BatchApexErrorEvent if it fails, so deliver the event.
11Test.getEventBus().deliver();

If further platform events are published by downstream processes, add Test.getEventBus().deliver(); to deliver the event messages for each process. For example, if a platform event trigger, which processes the event from the Apex job, publishes another platform event, add a Test.getEventBus().deliver(); statement to deliver the event message.

Note