+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

How to use a Email Template instead of direct message body

Hi,

I created one batch class, which is sending a notofication based on few conditions.

As of now i am sending a static message body as mentioned in the given code.

I would like to call email template instead of this static message body.

Can you please check my code and let me know where i should change my code.
 
global void finish(Database.BatchableContext BC){     
EmailTemplate emailTemplate = [Select id,name from EmailTemplate where name = 'GD Lead No Activity Email Notification Template' limit 1];
        for(GD_Lead__c lead : leadList){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] repEamail = new String[] {lead.GD_Assigned_To_Email__c};
            mail.setSaveAsActivity(false);
            mail.setTemplateID(emailTemplate.Id); 
            //mail.setTargetObjectId(lead.Id);
            mail.setWhatId(lead.Id);
            mail.setToAddresses(repEamail); 
            //mail.setccAddresses(managerEmail);
            mail.setSubject('No Activity Reminder');
            mail.setPlainTextBody('Hi  '+ lead.GD_Sales_Rep_Name__c+', \n \nPlease be informed, we have not found any activities on this Lead since last '+ days + ' days.\n \nKindly take an immediate action before escalate.  \n \n Lead Name : '+lead.Name +' \n \n Lead Number : '+ lead.Lead_Unique_Number__c +'\n \nRegards, \nGulf Drug Group.');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
    }

Thanks in advance,
Soundar.
Best Answer chosen by Soundar Raj Ponpandi
ShirishaShirisha (Salesforce Developers) 
Hi Soundar,

Greetings!

Have you checked the below thread which has the sample code to use the Email Template:

https://developer.salesforce.com/forums/?id=906F00000005IlPIAU

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Soundar,

Greetings!

Have you checked the below thread which has the sample code to use the Email Template:

https://developer.salesforce.com/forums/?id=906F00000005IlPIAU

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
This was selected as the best answer
Soundar Raj PonpandiSoundar Raj Ponpandi
Thanks Shirisha.