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
A S 31A S 31 

Send single email to a user with list of record Id's

Hi,

I have a requirement to send out a single email with a list of Record Id's that were inserted during the scheduled job to a single user.
Can any one tell me how to iterate through this list of records that were inserted and send a single email with all the record ID's to a user.

Thanks
 
Naval Sharma4Naval Sharma4
Hi,

You can send an email when you job gets executed and flow goes into finish method.
 
global class batchRecordInsert implements Database.Batchable<sObject>
{
    
    Set<String> insertedRecordIds = new Set<String>();

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        

    }

    global void execute(Database.BatchableContext BC, List<Contact> scope)
    {
         // Your code
    }  
    global void finish(Database.BatchableContext BC)
    {
     messaging.singleEmailMessage mail = new messaging.singleEmailMessage();
     AsyncApexJob a = [SELECT Id,
                             Status,
                             NumberOfErrors,
                             JobItemsProcessed,
                             TotalJobItems,
                             CompletedDate,
                             ExtendedStatus,
                             ApexClass.name,
                             CreatedBy.Email,
                             CreatedBy.Name
                      FROM AsyncApexJob 
                      WHERE Id =:BC.getJobId()];
    mail.ToAddresses = new string[]{ 'youremail@address.com'};
   // mail.setReplyTo('');   // <-----------------------------------Set the ReplyTo---
    mail.setSubject('Record Insertion Complete -- ');
    mail.setUseSignature(false);

    string htmlBody = '<div> ';
	htmlBody += '<table style="width:100%;"><tr> <th> Record Ids </th>';
	for(String id : insertedRecordIds){
		htmlBody += '<td> ' + id + '</td>';
	}
	htmlBody += '</tr> </table> </div>';
    mail.setHtmlBody(htmlBody);
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    mails.add(mail);
    Messaging.sendEmail(mails);

    }
}