Use BatchApexErrorEvent Triggers to Monitor ISV Applications With Example (Salesforce Spring ’21 Release)
Include BatchApexErrorEvent triggers in your managed package to monitor the health of batch jobs and take necessary corrective action without any post-installation steps.
Where: This change applies to Lightning Experience and Salesforce Classic.
How: The BatchApexErrorEvent object represents a platform event associated with a failing batch Apex execution. To fire this platform event, a batch Apex class declaration must implement the Database.RaisesPlatformEvents interface. A BatchApexErrorEvent platform event is fired when a batch Apex job encounters an unhandled exception. For more details, see Firing Platform Events from Batch Apex in the Apex Developer Guide.
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.
- public with sharing class YourSampleBatchJob implements Database.Batchable<SObject>,
- Database.RaisesPlatformEvents{
- // class implementation
- }
Example
- trigger MarkDirtyIfFail on BatchApexErrorEvent (after insert) {
- Set<Id> asyncApexJobIds = new Set<Id>();
- for(BatchApexErrorEvent evt:Trigger.new){
- asyncApexJobIds.add(evt.AsyncApexJobId);
- }
- Map<Id,AsyncApexJob> jobs = new Map<Id,AsyncApexJob>(
- [SELECT id, ApexClass.Name FROM AsyncApexJob WHERE Id IN :asyncApexJobIds]
- );
- List<Account> records = new List<Account>();
- for(BatchApexErrorEvent evt:Trigger.new){
- //only handle events for the job(s) we care about
- if(jobs.get(evt.AsyncApexJobId).ApexClass.Name == 'AccountUpdaterJob'){
- for (String item : evt.JobScope.split(',')) {
- Account a = new Account(
- Id = (Id)item,
- ExceptionType__c = evt.ExceptionType,
- Dirty__c = true
- );
- records.add(a);
- }
- }
- }
- update records;
- }
Yislentan_Rochester Sima Lenz https://wakelet.com/wake/TOSgNwSpC2WWzDcTdQkNy
ReplyDeleterecnalime
caegrancon-wa April Jones Internet Download Manager
ReplyDeleteWonderShare Recoverit
Vysor
aballance
AsupptaFinshi-Aurora Cyndi Peacock programs
ReplyDeleteClick
kizrisoper