You need to sign in to do that
Don't have an account?
Phuc 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?
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(); } }
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
Anudeep
All Answers
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
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