+ Start a Discussion
pranav shah 40pranav shah 40 

Custom Email Template Issue in Apex Class in Salesforce

Dear All, 

I have an requirement where i have to send an mail dynamically to user once review record  get created.
I have created custom HTML Email Template  but in blank body is going...
i am not getting html template which i have created

Below is the code which have written

 EmailTemplate template = [SELECT Id,Name,Description,HtmlValue,Body,Subject,DeveloperName 
                                  FROM EmailTemplate WHERE DeveloperName='Review_Email_Notifications'];
        
        System.debug('templates@@@'+template);
        
        String emailSubject = template.Subject;
         String emailTextBody = template.Body;
        if(!usersObjList.isEmpty()) {     
            for(user emails : usersObjList){
            //String emailSubject = template.getSubject();
                //String emailTextBody = template.getPlainTextBody();
             Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
              //  String[] toAddresses = new String[] {emails.email};  
                    //mail.setSenderDisplayName('Administrator');
                // mail.setToAddresses(toAddresses);
                mail.setSubject(emailSubject);
                mail.setPlainTextBody(emailTextBody);
                //mail.setTreatBodiesAsTemplate(true);
                                
                //mail.setplainTextbody(emailTextBody);  
                mail.setWhatId(varReview.id);    
                mail.setTemplateId(template.Id);
                mail.setSaveAsActivity(false); 
                mail.setTargetObjectId(emails.id); //0030m00000aeYpdAAE
                
               //mail.setTargetObjectId(ReviewId); //0030m00000aeYpdAAE
                Messaging.SendEmailResult[] results=Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
                
            }
        }

please help
AnudeepAnudeep (Salesforce Developers) 
Can you try with a simpler version of code to narrow this down?
 
EmailTemplate et = [SELECT Id,Subject, Body FROM EmailTemplate WHERE DeveloperName =:emailTemplateName];
List<string> toAddress = new List<string>();
toAddress.add(primaryEmail);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setTemplateId(et.Id);
    mail.setToAddresses(toAddress);
    mail.setSubject(et.subject);
    mail.setHTMLBody(et.Body);
    mail.setTargetObjectId(primaryContact);
    mail.setWhatId(primaryAccount);
    mail.setSaveAsActivity(false);
    mail.setUseSignature(false);
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
allmsg.add(mail);

try {
    Messaging.sendEmail(allmsg,false);
    return;
} catch (Exception e) {
    System.debug(e.getMessage());
}