+ Start a Discussion
Rabbani sayyed 8Rabbani sayyed 8 

I have a requirement when a batch is running and if there is some failure occuring in the records ,a msg should be sent .How do i achieve this? any suggestions on this ?

I have a requirement when a batch is running and if there is some failure occuring in the records ,a msg should be sent .How do i achieve this? any suggestions on this ?
Best Answer chosen by Rabbani sayyed 8
Himanshu ParasharHimanshu Parashar
To achieve this you need to follow below steps.

1. Implement Database.Stateful in your batch
2. Manage list of errors in a list
3. send email inside finish method with all error details (single email.)

here is one sample code for this :
 
global class BATCHNAME implements Database.Batchable<sObject>, Database.Stateful{
global List<String> lstError;

global DeactivateUserOnExpiry_BatchClass(){         
        lstError = new List<String>();   
}
global void execute(Database.BatchableContext BC, List<Account> accUpdate)
{        
       
        capture failed results
        Database.SaveResult[] SaveResultList = Database.update(accUpdate,false);             
        for(integer i =0; i<accUpdate.size();i++)
        {
            errormsg=accUpdate.get(i).id + ':';
            If(!SaveResultList[i].isSuccess()){
                 for(Database.Error err: SaveResultList[i].getErrors()){  
                         errormsg += err.getmessage()+'"\n\n';
                    } 
                lsterror.add(errormsg); //add to list for future use.
            }
       }    
  }
  
   global void finish(Database.BatchableContext BC){  
       //iterate list and send email.
       if(lsterror.size()>0){  
             String emailbody='';
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                    
            String[] toAddresses = new String[] {'test@domain.com'}; 
            mail.setSubject('Errors occurred.');            
            mail.setToAddresses(toAddresses);
            mail.setUseSignature(false);
            for(integer i =0 ; i<Error_msg_List.size();i++ ){   
                emailbody+= lsterror.get(i);                
            } 
            mail.setPlainTextBody(emailbody);     
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });   //send email
       }
      
   }
}


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S.  If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

All Answers

Himanshu ParasharHimanshu Parashar
To achieve this you need to follow below steps.

1. Implement Database.Stateful in your batch
2. Manage list of errors in a list
3. send email inside finish method with all error details (single email.)

here is one sample code for this :
 
global class BATCHNAME implements Database.Batchable<sObject>, Database.Stateful{
global List<String> lstError;

global DeactivateUserOnExpiry_BatchClass(){         
        lstError = new List<String>();   
}
global void execute(Database.BatchableContext BC, List<Account> accUpdate)
{        
       
        capture failed results
        Database.SaveResult[] SaveResultList = Database.update(accUpdate,false);             
        for(integer i =0; i<accUpdate.size();i++)
        {
            errormsg=accUpdate.get(i).id + ':';
            If(!SaveResultList[i].isSuccess()){
                 for(Database.Error err: SaveResultList[i].getErrors()){  
                         errormsg += err.getmessage()+'"\n\n';
                    } 
                lsterror.add(errormsg); //add to list for future use.
            }
       }    
  }
  
   global void finish(Database.BatchableContext BC){  
       //iterate list and send email.
       if(lsterror.size()>0){  
             String emailbody='';
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                    
            String[] toAddresses = new String[] {'test@domain.com'}; 
            mail.setSubject('Errors occurred.');            
            mail.setToAddresses(toAddresses);
            mail.setUseSignature(false);
            for(integer i =0 ; i<Error_msg_List.size();i++ ){   
                emailbody+= lsterror.get(i);                
            } 
            mail.setPlainTextBody(emailbody);     
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });   //send email
       }
      
   }
}


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S.  If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
This was selected as the best answer
Rabbani sayyed 8Rabbani sayyed 8
Thank you very much Himanshu

Regards
Rabbani