You need to sign in to do that
Don't have an account?
Saswat Mohanty
status of batch job to be sent in one email
Hi,
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])
{
system.debug('list------------------------'+a);
//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')
{
ls.add(aC.Name);
ls.add(a.status);
setofsucuss.add(aC.Name);
setofsucuss.add(String.valueof(a.CreatedDate));
system.debug('set of records ----------------'+setofsucuss);
listofrecords.add(ls);
}
if(a.NumberOfErrors!=0 & a.Status=='Completed')
{
failed.add(aC.Name);
failed.add(a.status);
failed.add(a.ExtendedStatus);
Totalfailedrecords.add(failed);
}
//ls.clear();
if(a.NumberOfErrors==0 & a.Status=='Processing')
{
onprocess.add(aC.Name);
onprocess.add(a.status);
Totalonprocessrecords.add(onprocess);
}
}
system.debug('listofrecords----------------'+listofrecords);
//sendmailnofication();
}
//delete till yesterday batch records.
/*public PageReference methodtodeleteoldjobs()
{
Integer count = System.purgeOldAsyncJobs(Date.today());
System.debug('-----------Deleted------------------ ' + count + ' old jobs.');
return null;
}*/
//mail notification
public void sendmailnofication()
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'saibabu.salesforce@gmail.com','saswat-kumar.x.mohanty@gsk.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('sm00364377@techmahindra.com');
mail.setSenderDisplayName('Salesforce Support');
mail.setSubject('Status of Batch classes : ');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setHtmlBody('The below batch clasees are executed sucussfully with the latest time stamp: '+setofsucuss);
system.debug('----------------------------2---------------');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
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])
{
system.debug('list------------------------'+a);
//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')
{
ls.add(aC.Name);
ls.add(a.status);
setofsucuss.add(aC.Name);
setofsucuss.add(String.valueof(a.CreatedDate));
system.debug('set of records ----------------'+setofsucuss);
listofrecords.add(ls);
}
if(a.NumberOfErrors!=0 & a.Status=='Completed')
{
failed.add(aC.Name);
failed.add(a.status);
failed.add(a.ExtendedStatus);
Totalfailedrecords.add(failed);
}
//ls.clear();
if(a.NumberOfErrors==0 & a.Status=='Processing')
{
onprocess.add(aC.Name);
onprocess.add(a.status);
Totalonprocessrecords.add(onprocess);
}
}
system.debug('listofrecords----------------'+listofrecords);
//sendmailnofication();
}
//delete till yesterday batch records.
/*public PageReference methodtodeleteoldjobs()
{
Integer count = System.purgeOldAsyncJobs(Date.today());
System.debug('-----------Deleted------------------ ' + count + ' old jobs.');
return null;
}*/
//mail notification
public void sendmailnofication()
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'saibabu.salesforce@gmail.com','saswat-kumar.x.mohanty@gsk.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('sm00364377@techmahindra.com');
mail.setSenderDisplayName('Salesforce Support');
mail.setSubject('Status of Batch classes : ');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setHtmlBody('The below batch clasees are executed sucussfully with the latest time stamp: '+setofsucuss);
system.debug('----------------------------2---------------');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
Hi Saswat ,
why don't you use apex scheduler and monitor it through the setup . take a look to :
https://help.salesforce.com/HTViewHelpDoc?id=code_schedule_batch_apex.htm&language=en_US
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.html
Best regards ,