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
Arnold Joseph TodasArnold Joseph Todas 

Batch Email Message using template

How can i call the email template in batch apex class and send an email?

Thanks

AJ
 

KaranrajKaranraj
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    
Id templateId =  [select id, name from EmailTemplate where developername = 'templateName].id; //replace with your template name
    
email.setToAddresses(toRecipients);
email.setCcAddresses(ccRecipients);
// Who you are sending the email to
email.setTargetObjectId(targetObjId);
email.setWhatId(whatId); //Your Record Id if applicable
email.setorgWideEmailAddressId(orgWideEmailId);
email.setTemplateId(templateId);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
You can directly the query the EmailTemplate object using SOQL query to the template ID and assign that in your email message.