You need to sign in to do that
Don't have an account?
Pradeep d
Why Templateid is not working in my batch class
Hi folks,
template is not sending with body in single email batch apex
global class CampaignMassMailbatch implements Database.batchable<sobject>, Database.Stateful { public Campaign camp; public id campid; public string CampName; public date startdat; public date enddat; public id Ownerid; public string descript; public string query; global Database.QueryLocator start(Database.BatchableContext bc) { query = 'SELECT id, name, email from Campaignmember where Campaign.id =\'' +campid+ '\'' ; system.debug(Database.getQueryLocator(query)); return Database.getQueryLocator(query); } global void execute(Database.BatchableContext bc, List<Campaignmember> Scope){ system.debug('campaign batch'); List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>(); for(CampaignMember cm : Scope){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage() ; EmailTemplate et=[Select id from EmailTemplate where developername = 'Campaign Invitation' limit 1]; system.debug(et.id); String[] toAddresses = new String[] {cm.Email} ; mail.setToAddresses(toAddresses) ; mail.setSubject('Campaign Invitation'); // mail.setHtmlBody('body'); mail.setTemplateId(et.id); mails.add(mail); } Messaging.SendEmailResult[] results = Messaging.sendEmail(mails); if (results[0].success) { System.debug('The email was sent successfully.'); } else { System.debug('The email failed to send: ' + results[0].errors[0].message); } } global void finish(Database.BatchableContext bc){ } }
Please make sure 'Available for use checkbox checked in email templates.
Thanks,
Saravana
Please refer the below link which have things to remember when using template id in apex.
The link says "You need not worry about targetObjectId and whatId when you deal with plain text emails. These properties are important when email is fired using an email template in salesforce"
https://suyati.com/blog/templated-emailmessaging-through-apex-code-things-to-remember/ (https://suyati.com/blog/templated-emailmessaging-through-apex-code-things-to-remember/" target="_blank)
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Thanks,
Sarav
Sweet Potato Tec
Please refer this link if it helps.
https://salesforce.stackexchange.com/questions/165683/receiving-empty-email
Thanks,
Sarav
Sweet Potato Tec
Have you set the whatid. If not the merge fields won't pull the value.
Link - https://developer.salesforce.com/forums/?id=906F000000090IZIAY
Thanks,
Sarav
Sweet Potato Tec