+ Start a Discussion
Hemanth SayanaHemanth Sayana 

Batch job result email with all failed records and respective error message

Hi,
I have a scenario where i have a batch class. Once batch job is done I need to send an email to specific email id with excel file. This excel file should contain all the records that got failed in the batch job along with error message. Please suggest.
Linga_RaminLinga_Ramin
Hi Hemanth ,You can go through this link.This will solves your problem

https://developer.salesforce.com/forums/?id=906F0000000AVZ9IAO
Ajay K DubediAjay K Dubedi
 Hi Hemanth,
 I have created a batch Class and fetch all the Leads created yesteday not having a Phone number.
Send an Email to the system Admin with the list of those Leads with detail.
   
global class SendEmail implements Database.Batchable<sObject> {
    global Database.QueryLocator start (Database.BatchableContext bc){
        return Database.getQueryLocator('SELECT Id, Name,OwnerId FROM Lead WHERE Phone = null ');
    }
    global void execute(Database.BatchableContext bc, List<Lead> Leadlist){
        system.debug('LeadList>>>>>>'+Leadlist);
       Set<Id> User_Id = new Set<Id>();
        for(Lead ld : Leadlist){
            User_Id.add(ld.OwnerId);
        }
        List <User> user_info = new  List <User>();
        user_info=[SELECT Id,Name,Email FROM User WHERE Id In: User_Id];
       
       
        if(user_info.size()>0){
              Map<Id,List<Lead>> leadnamevscomp = new Map<Id,List<Lead>>();
            for(Lead ld : Leadlist){
                if(!leadnamevscomp.containsKey(ld.OwnerId)){
                  leadnamevscomp.put(ld.OwnerId,new List<Lead>()); 
                }
                leadnamevscomp.get(ld.OwnerId).add(ld);
                
            }
            List<Messaging.SingleEmailMessage> message_content = new List<Messaging.SingleEmailMessage>();
            for(User us:user_info){
                Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
                msg.toaddresses = new String[]{us.Email};
                    msg.subject = 'Lead-status-update';
                msg.plaintextbody = 'Updation has been completed'+'  '+
                    'Details of Lead    '+leadnamevscomp.get(us.Id);
                    message_content.add(msg); 
            }
           
            Messaging.SendEmailResult[] results = Messaging.sendEmail(message_content);
   if (results[0].success) { 
                        System.debug('The email was sent successfully.');
                       
                    } else {
                        System.debug('The email failed to send: '+ results[0].errors[0].message);
                       
                    }
        }
    }
    global void finish(Database.BatchableContext bc){
        system.debug('Done');
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Hemanth SayanaHemanth Sayana
Thanks for the response. Please find below question with some more details. I am updating all the account records through batch class. Now while updating some of the records got failed. I want Id and name of all the failed records along with the error message saying why the update has been failed. This data i have to send email in excel format.