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
Phuc Nguyen 18Phuc Nguyen 18 

batch class send email with email template

Hello all,
I am trying to send out emais when the record meets a specific criteria and I need to use an exsiting email template.  How do I use the template and make sure I am grabbing the fields for the tempalte?
global class UpdateAccountBillingState implements Database.Batchable<sObject>, Database.Stateful {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT notifydate__c FROM Account Where notifydate__c != null'
        );
    }
    global void execute(Database.BatchableContext bc, List<Account> scope){
        // process each batch of record
        List<Account> lstacc = new List<Account>();       
        for (Account acc : scope) {
            acc.notifydate__c = today();
            lstacc.add(acc);  
        }   
        update lstacc;
    }   
    global void finish(Database.BatchableContext bc){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      
    }   
     
}

 
Best Answer chosen by Phuc Nguyen 18
AnudeepAnudeep (Salesforce Developers) 
Hi Phuc, 

Use the setTemplateId field and use the ID of the template you want to send. Also, use setTargetObjectId(targetObjectId) to ensure that merge fields in the template contain the correct data. See documentation for more details

Here is a sample code
 
Contact c = [select id, Email from Contact where email <> null limit 1];

List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
msg.setTemplateId( [select id from EmailTemplate where DeveloperName='My_Email_Template'].id );
msg.setWhatId( [select id from Account limit 1].id );
msg.setTargetObjectId(c.id);
msg.setToAddresses(new List<String>{'random_address@test.com'});
lstMsgs.add(msg);
Savepoint sp = Database.setSavepoint();
Messaging.sendEmail(lstMsgs);
Database.rollback(sp);

Anudeep

 

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Phuc, 

Use the setTemplateId field and use the ID of the template you want to send. Also, use setTargetObjectId(targetObjectId) to ensure that merge fields in the template contain the correct data. See documentation for more details

Here is a sample code
 
Contact c = [select id, Email from Contact where email <> null limit 1];

List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
msg.setTemplateId( [select id from EmailTemplate where DeveloperName='My_Email_Template'].id );
msg.setWhatId( [select id from Account limit 1].id );
msg.setTargetObjectId(c.id);
msg.setToAddresses(new List<String>{'random_address@test.com'});
lstMsgs.add(msg);
Savepoint sp = Database.setSavepoint();
Messaging.sendEmail(lstMsgs);
Database.rollback(sp);

Anudeep

 
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
Thanks Anudeep,
Now will the code go in the execute or finish method?  Or does it matter?
If want the setToAddresses to be a field on the Account record would just replace the 'random_address@test.com' with c.emailaddress__c?

Thank you,
P