Wednesday, November 30, 2022

Send an email having Apex Batch Job details after execution

 //Send an email post number of records processed successfully 

global class BatchClassName implements Database.Batchable<SObject>,Database.Stateful {

//get data in start method from querylocator

    global Database.querylocator start(Database.BatchableContext bc){

        //Query logic here

String queryStr = 'Select Query Here';

        return Database.getQueryLocator(queryStr);

    }

  //perform action in execute method on the data extracted from start method

    global void execute(Database.BatchableContext bc, List<Case> scope) {

        System.debug('scope @@@ '+scope);

        //execute logic here

    }

    // at last finish method execute to send email about execution

    global void finish(Database.BatchableContext bc) {

        

      Messaging.SingleEmailMessage m_sem = new Messaging.SingleEmailMessage();

      AsyncApexJob a = [Select a.TotalJobItems,a.Status,a.NumberOfErrors,a.JobType,a.JobItemsProcessed,a.ExtendedStatus From AsyncApexJob a WHERE id = :BC.getJobId()];

      System.debug('$$$ Jobid is '+BC.getJobId());

      List<String> emails = new List<String>{'to_emailaddress@gmail.com'};

      m_sem.setToAddresses(emails);

      m_sem.setReplyTo('reply_emailaddress@gmail.com'); 

      m_sem.setSenderDisplayName('SFDC');

      m_sem.setSubject('Processed Batch Job Status '+a.Status);

      m_sem.setPlainTextBody('The batch job has been processed: '+a.Status+' Total Job Items: '+ a.TotalJobItems+', Failures: '+a.NumberOfErrors+', Job Item processed: '+a.JobItemsProcessed+'.');

      Messaging.sendEmail(new Messaging.SingleEmailMessage [] {m_sem});

    }

}

No comments:

Post a Comment

Validation in flow input text field in sfdc: Maximum 255 characters limit

The input text field validate in sfdc flow behaves opposite of the generic sfdc validation rule.  Here the validation formula is evaluating ...