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

How to send out an email to separate addresses in a batch.

I need to send an email out to whoever fits the criteria of my sched batch. The batch runs nightly. So I will need it to email different people each time. Also, I need to include a link to the record they should be adjusting and the record link will be specific to them. What's the best way to do this in my batch?
Ajay K DubediAjay K Dubedi
Hey AnonTestQuest,
         In the finish logic of the batch you can implement this block of code this is for sending out csv's :
As per your requirement you can query new email address each time the batch executes and put it in a string and pass in place of 'hardcodedemail@gmail'
 I think this will solve your problem of sending email each time to a new person. 
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
        Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
        String[] toAddresses = new list<string> {''}; // Pass the result of your latest query for email here 
        String subject ='Subject of CSV';
        email.setToAddresses( toAddresses );
        email.setPlainTextBody(' CSV ');
        email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});