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
Pradeep dPradeep 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){
       
      }    


}
 

 

Saravana Muthu 8Saravana Muthu 8
Hi,

Please make sure 'Available for use checkbox checked in email templates.

Thanks,
Saravana
Pradeep dPradeep d
which is enabled already
Saravana Muthu 8Saravana Muthu 8
Hi,

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
Pradeep dPradeep d
Thanks Sarav, My actual requirement is Dear {!Candidate__c.Name }, We request you to please attend to the {!Campaign.Name } Please find the details of the campaign below. Campaign Name: {!Campaign.Name } Campaign Owner: {!Campaign.OwnerId} Start Date: {!Campaign.StartDate} End Date: {!Campaign.EndDate} is my template, whenever i try to send with batch, the values are not populating, just getting blank
Saravana Muthu 8Saravana Muthu 8
Hi,

Please refer this link if it helps.

https://salesforce.stackexchange.com/questions/165683/receiving-empty-email

Thanks,
Sarav
Sweet Potato Tec
Pradeep dPradeep d
No, I dint include setsubject EmailTemplate et=[Select id from EmailTemplate where name = 'Campaign Invitation2' limit 1]; Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage(); mail.setTargetObjectIds(lstIds); mail.setSenderDisplayName('Campaign Invitation'); mail.setTemplateId(et.id); Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
Pradeep dPradeep d
Here is my template,

Dear {!Candidate__c.Name}, 

We request you to please attend to the {!Campaign.Name} 

Please find the details of the campaign below. 

Campaign Name: {!Campaign.Name} 
Campaign Owner: {!Campaign.OwnerId} 
Start Date: {!Campaign.StartDate} 
End Date: {!Campaign.EndDate} 

Am receiving like this 

Dear ,

We request you to please attend to the

Please find the details of the campaign below.

Campaign Name:
Campaign Owner:
Start Date:
End Date:

 
Saravana Muthu 8Saravana Muthu 8
Hi,

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