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
Saswat MohantySaswat Mohanty 

status of batch job to be sent in one email


Currently in our org there are many batch jobs running. Once they are completed successfully, we need to send a consolidated email of all batch classes. The problem i am facing is once they are successfully completed, i am getting multiple times emails once they are finished. So, let's say if i have a batch class on Account to update a field and if i update 2 accounts, the batch job runs 2 times and sends email 2 times. I want only one email out of it for a whole day.

Could you help please with this code?

public class batchdataviewcon {

    public List<List<String>> getTotalonprocessrecords() {
        return Totalonprocessrecords;

    public List<List<String>> getTotalfailedrecords() {
        return Totalfailedrecords;

    public List<List<String>> getListofrecords() {
        return listofrecords;

public List<List<String>> listofrecords;
public List<List<String>> Totalfailedrecords;
public List<List<String>> Totalonprocessrecords;
public Set<String> setofsucuss;
public batchdataviewcon()
 listofrecords=new List<List<String>>();
 Totalfailedrecords=new List<List<String>>();
 Totalonprocessrecords=new List<List<String>>();
 setofsucuss=new Set<String>();

 for(AsyncApexJob a : [select TotalJobItems, Status, NumberOfErrors, MethodName, JobType, JobItemsProcessed, ExtendedStatus, Id, CreatedDate, CreatedById, CompletedDate, ApexClassId From AsyncApexJob WHERE JobType='BatchApex' order by CreatedDate desc])
  //success list
  List<String> ls=new List<String>();
  //failed list
   List<String> failed=new List<String>();
   //processing list
   List<String> onprocess=new List<String>();
  ApexClass aC = [SELECT Id, Name FROM ApexClass WHERE Id =:a.ApexClassId];
  system.debug('batch class name----------------'+aC);
  if(a.NumberOfErrors==0 & a.Status=='Completed') 
    system.debug('set of records ----------------'+setofsucuss);
  if(a.NumberOfErrors!=0 & a.Status=='Completed')
  if(a.NumberOfErrors==0 & a.Status=='Processing')


 //delete till yesterday batch records.
 /*public PageReference methodtodeleteoldjobs()
 Integer count = System.purgeOldAsyncJobs(;
 System.debug('-----------Deleted------------------ ' + count + ' old jobs.');
  return null;
 //mail notification
 public void sendmailnofication()
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   String[] toAddresses = new String[] {'',''};
   mail.setSenderDisplayName('Salesforce Support');
   mail.setSubject('Status of Batch classes : ');
   mail.setHtmlBody('The below batch clasees are executed sucussfully with the latest time stamp: '+setofsucuss);
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

Kamal BelayacKamal Belayac

Hi Saswat , 
why don't you use apex scheduler and monitor it through the setup . take a look to :

Best regards ,