Get Hands-on Using Platform Events from Batch Apex Classes
Requirement : Modify an existing batch Apex job to raise BatchApexErrorEvents
Take an existing batch Apex job class and update it to implement the Database.RaisesPlatformEvents interface. Then, add a trigger on BatchApexErrorEvent that logs exceptions in the batch job to a custom object.
- Update the BatchLeadConvert class to implement the Database.RaisesPlatformEvents marker interface.
- Create an Apex trigger called BatchApexErrorTrigger on the BatchApexErrorEvent SObject type. For each event record, capture the following fields and save them to the corresponding fields in a new BatchLeadConvertErrors__c record.
- AsyncApexJobId: AsyncApexJobId__c
- JobScope: Records__c
- StackTrace: StackTrace__c
- To make the trigger bulk safe, use a single DML statement to insert a list of new records at the end.