I have create a Lightning email template related to Account. In the apex code I am referencing this email template, so that the email that goes out is based on this template. The email I receive is not the the template but what i have hard coded in the subject and body in the apex code.
[][1
What changes do i make so that the email that goes out is based on the email template?
public static void SendMail(String email,String recordId){
EmailTemplate emailTemplate = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where name = 'VerifyDuplicateAccount'];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{email};
mail.setToAddresses(toAddresses);
mail.setSubject('Verify duplicate Account');
mail.setPlainTextBody('Hi, This record is duplicate : '+recordId);
mail.setBccSender(false);
mail.setUseSignature(false);
mail.saveAsActivity = false;
mail.setTemplateID(emailTemplate.Id);
mail.setWhatId(recordId); //This is important for the merge fields in template to work
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
setTargetObjectId(targetObjectId)
where targetObjectId is the ID of the contact, lead, or user to which the email will be sent.