function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nitin sharma 425Nitin sharma 425 

Platform events and Trigger to log errors

Hi All,
Firstly,I am not sure if the Raise Paltform event has been released for all or not.Does anyone knows about it?

Another question:-
I wrote a batch class and deliberately generated an exception while updating an Account record.I did not provide an Account Id while updating an Account record...I was hoping it will fire a trigger on the BatchApexErrorEvent and then update the accoun object with the type of Exception..But  triggetr is not getting Fired.Am sure I am missing something here.Please advice.

Simple Batch Apex:-
global class AccountBatchApex implements Database.Batchable<sObject>,Database.RaisesPlatformEvents{
  //Database.RaisesPlatformEvents   
    //global integer numberofDirectCustomers = 0;
    
     //integer counter=0;
    global Database.QueryLocator start(Database.BatchableContext bc){
        String soqlQuery = 'SELECT Name, AccountNumber, Type From Account limit 10';
        return Database.getQueryLocator(soqlQuery);
    }
     
    global void execute(Database.BatchableContext bc, List<Account> scope){
        List<Account>acc12=new list<Account>();
        for (Account acc : scope){
            
            Account a=new account(Name='NYTimes',AccountNumber='1234567890');
            acc12.add(a);
            
            
        }
        Update Acc12;
    }
     
    global void finish(Database.BatchableContext bc){
         
    }
}

Trigger Code which is from the guide.

global class AccountBatchApex implements Database.Batchable<sObject>,Database.RaisesPlatformEvents{
  //Database.RaisesPlatformEvents   
    //global integer numberofDirectCustomers = 0;
    
     //integer counter=0;
    global Database.QueryLocator start(Database.BatchableContext bc){
        String soqlQuery = 'SELECT Name, AccountNumber, Type From Account limit 10';
        return Database.getQueryLocator(soqlQuery);
    }
     
    global void execute(Database.BatchableContext bc, List<Account> scope){
        List<Account>acc12=new list<Account>();
        for (Account acc : scope){
            
            Account a=new account(Name='NYTimes',AccountNumber='1234567890');
            acc12.add(a);
            
            //if(acc.Type.equals('Customer - Direct'))
            //{
               // numberofDirectCustomers++;
                //System.debug('The number of new cutomer are as follows'+numberofDirectCustomers);
                
            //}
        }
        Update Acc12;
    }
     
    global void finish(Database.BatchableContext bc){
         
    }
}



Once again:-Is BatchApexErrorEvent  is still Beta or available to all users.?

Is it possible to query this object?


 










 
Nitin sharma 425Nitin sharma 425
Sorry,

Forgot to add trigger along with the Batch class.

global class AccountBatchApex implements Database.Batchable<sObject>,Database.RaisesPlatformEvents{
  //Database.RaisesPlatformEvents   
    //global integer numberofDirectCustomers = 0;
    
     //integer counter=0;
    global Database.QueryLocator start(Database.BatchableContext bc){
        String soqlQuery = 'SELECT Name, AccountNumber, Type From Account limit 10';
        return Database.getQueryLocator(soqlQuery);
    }
     
    global void execute(Database.BatchableContext bc, List<Account> scope){
        List<Account>acc12=new list<Account>();
        for (Account acc : scope){
            
            Account a=new account(Name='NYTimes',AccountNumber='1234567890');
            acc12.add(a);
            
            
        }
        Update Acc12;
    }
     
    global void finish(Database.BatchableContext bc){
         
    }
}


Here it comes.


trigger MarkDirtyIfFail on BatchApexErrorEvent (after insert) {
    Set<Id> asyncApexJobIds = new Set<Id>();
    for(BatchApexErrorEvent evt:Trigger.new){
        System.debug('I am in for loop of the Trigger'+evt);
        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
        //
        System.debug('I am in a another for loop of the Trigger'+evt);
        if(jobs.get(evt.AsyncApexJobId).ApexClass.Name == 'AccountBatchApex'){
            for (String item : evt.JobScope.split(',')) {
                System.debug('I am in the third for loop'+item);
                Account a = new Account(
                    Id = (Id)item,
                    ExceptionType__c = evt.ExceptionType,
                    Dirty__c = true
                );
                records.add(a);
            }
        }
    }
    update records;
}


Once again:-Is BatchApexErrorEvent  is still Beta or available to all users.?

Is it possible to query this object?