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
VSK98VSK98 

Can i use two template 's in the single message and need to call two template's in the batch class

Hi,

I have used one email template in the single messaging and called in the batch class .........Now i need to call another email template in the batch class based on the condition like in start method 
for eg: string soql = 'select id , Name from Account where Name = 'siva' ';
string soql 3 = 'select id , Name from Account where Name = 'sivakumar' ';

here i used one email template in the batch class
global class  WTPAA_sendReminderwagenoticestoemployees Implements Database.Batchable <sObject> {
    global Database.queryLocator start(Database.BatchableContext bc) {
        
        Date d = Date.today();
     //   String SOQL = 'SELECT Id, WTPAA_Employee_Email__c,WTPAA_Related_To__r.id,WTPAA_Status__c,WTPAA_Reminder_for_2nd_week__c,WTPAA_Reminder_for_3rd_week__c,WTPAA_Reminder_for_4nd_week__c FROM WTPAA_Wage_Notice__c WHERE (WTPAA_Reminder_for_2nd_week__c OR WTPAA_Reminder_for_3rd_week__c OR WTPAA_Reminder_for_4nd_week__c = :d) AND WTPAA_Status__c =  sent ';
        String SOQL = 'SELECT Id,WTPAA_Company_Name__c, WTPAA_Employee_Email__c,WTPAA_Related_To__r.id,WTPAA_Is_same_Employer_Mailing_Address__c FROM WTPAA_Wage_Notice__c WHERE WTPAA_Is_same_Employer_Mailing_Address__c = false';
        return Database.getQueryLocator(SOQL);
    }

    global void execute(Database.BatchableContext bc, List<WTPAA_Wage_Notice__c> wagenotice) {
        List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
       Emailtemplate et = [select id, developername , IsActive from Emailtemplate where developername = 'WTPAA_Reminder_for_2_weeks' AND IsActive = true];
      
        for(WTPAA_Wage_Notice__c w: wagenotice) {
           system.debug('size of the list' +wagenotice.size());
          //  List<String> toAddresses = new List<String>{w.WTPAA_Employee_Email__c};           
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          //  mail.setToAddresses(toAddresses);
          
            mail.SetTemplateid(et.id);
            mail.setSaveAsActivity(false);
          
           mail.setTargetObjectId(w.WTPAA_Related_To__r.id);
           system.debug('ID of Contact' +w.WTPAA_Related_To__r.id);
           mail.setWhatid(w.id);
        for(OrgWideEmailAddress owa : [select id, Address from OrgWideEmailAddress]) {

           if(owa.Address.contains('replicastatelabor@gmail.com')) {
            mail.setOrgWideEmailAddressId(owa .id);
           }
          } 
           
            system.debug('IDDDDDDDDDDD*******' +et.id);
            mailList.add(mail);   
               
        } 
        Messaging.sendEmail(mailList);        
    }

    global void finish(Database.BatchableContext bc) {
    }
}

please help me out.....................................................................................................
Jorge OrtegaJorge Ortega
Hi Siva,

Declare a global class attribute to store the template name and a constructor reciving the value.
global class MyBatchableClass implements Database.Batchable<sObject> {

 global String templateName;

 global MyBatchableClass(String tName) {
   this.templateName = tName;
 }

 global void execute(Database.BatchableContext bc, List<sObject> scope) {
   System.debug('Here is my template name: '+templateName);
 }
....
You must use global attributes if you want it to retain values when execute method is called.

Hope this help,
Jorge.
 
VSK98VSK98
Hi Jorge,

This is not what i expect unfortunately..........

Can i used two template in one batch class
Jorge OrtegaJorge Ortega
I don't see why you can not, what's the error you have?

- At the constructor
Try to obtain the two email templates, reducing queries.
Use a global Map<String, String> to store the template name and the template Id.

- At the execute method
Use a Map<String,List<Messaging.SingleEmailMessage>> to store the template Ids and the template's corresponding messages list
Invoke Messaging.sendEmail(mailing.get(templateId)) for every map entry (mailing is the map variable)
 
global Map<String,String> templates;
global Map<String,List<Messaging.SimpleEmailMesage>> mailing;
....
global MyBatchableClass implements Database.Batchable<sObject> {

global MyBatchableClass() {
  templates = new Map<String,String>();
  for(EmailTemplate et : [SELECT Id, developerName from .... ] ) {
    templates.put(et.developerName, et.Id);
  }
}

global void execute(Database.BatchableContext bc, List<sObject> scope) {
  mailing = new Map<String,List<Messaging.SimpleEmailMesage>>();
  for(String devName : templates.keySet()) {
     mailing.put(templates.get(devName),new List<Messaging.SimpleEmailMesage>());
  }
  for(sObject obj : scope) {
       // Construct the email, var singleEmail
     
       // Check condition and assign the correct template
       String tName;
       if(condition) {
          tName = 'templateName1';
       } else {
          tName = 'templateName2';
       }
       singleEmail.templateId = templates.get(tName);
       mailing.get(templates.get(tName)).add(singleEmail);
  }
  //Invoke sendMail for each template
  for(String tId : mailing.keySet()) {
      Messaging.sendEmail(mailing.get(tId));
  }
}

If you are worried about limits...Salesforce count every single mail is delivered, not the number of times you call sendMail.