+ Start a Discussion
VSrinivasanVSrinivasan 

Salesforce email

Hi,

 

I have written a batch class to send an email when there is a mismatch between two fields, In Execute method i have added the list of records having a mismatch in a list. I need to send the list as an attachment in a single mail. In the below code i am sending the mail in execute method, so for each and every mismatch record i am getting individual mail. But instead i need the whole list to be inserted in the mail body or as an attachment and send only one mail. Mail should not be sent for individual records.

 

Please let me know how to send only one mail with the list of records inserted in mail body or as an attahment and also let me know how to pass the list of records present in execute method to finish method.

 

PFB my sample code,

 

global class SendMail implements Database.Batchable<SObject>

{
List<User> usersList = new List<User>();


global Database.QueryLocator start(Database.BatchableContext BC)

{

return Database.getQueryLocator('select id,Name,EmployeeNumber,NewEmail__c,Testing__c,Email from User');

}

global void execute(Database.BatchableContext BC,List<Sobject> scope)

{

for(Sobject s : scope)

{
    User a = (User)s;
    if(a.EmployeeNumber != a.Testing__c || a.Email != a.NewEmail__c )
    usersList.add(a);
}

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

String[] toAddresses = new String[] {'Srinivasanece@gmail.com'};

mail.setToAddresses(toAddresses);

mail.setSubject('Apex Batch Job is done');

For(User p:usersList){

mail.setPlainTextBody('The batch Apex job processed'+p.id);

}

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

}

 

global void finish(Database.BatchableContext BC)

{
/*Mail to User mentioned in the "toAddress"*/   
/*Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

String[] toAddresses = new String[] {'srinivasanece90@gmail.com'};

mail.setToAddresses(toAddresses);

mail.setSubject('Apex Batch Job is done');

mail.setPlainTextBody('The batch Apex job processed');

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
*/
}

}

 
chiranjeevitgchiranjeevitg

Can you try this example code below in your controller based on this you can modify your batch class

 

List<Object> objlist = [Select id,name ,Fields from Object];
        string header = 'Record Id, Name , \n';
        string finalstr = header ;
        for(Export__c a: acclist)
        {
        
               string recordString = a.id+','+a.Name+'\n';
        
               finalstr = finalstr +recordString;
        
        }
        
        Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
        blob csvBlob = Blob.valueOf(finalstr);
        string csvname= 'Export.csv';
        csvAttc.setFileName(csvname);
        csvAttc.setBody(csvBlob);
        Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
        String[] toAddresses = new list<string> {'email address'};
        String subject ='Account CSV';
        email.setSubject(subject);
        email.setToAddresses( toAddresses );
        email.setPlainTextBody('Account CSV ');
        email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }