• Ankit Agrawal 28
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
hi all,

i have a scenario where in i query an html email template and populate its merge fields in my apex class itself.
however,when i use the sethtmlBody() method,it renders an empty email.
So i have to comment the sethtmlbody() and go with setplaintextbody().

What can be the reason for this?
Also how do i use the setHtmlBody() along with setplainTextBody()?

Below is my code
 
public static void sendingEmail(String cid,String cname,String eventId)
    {
        String plainTxtBody='',startDate='',endDate='',scheduledDate='';
        String hbody='';
        Date sdate;

        //get the email template
        EmailTemplate template=[SELECT HtmlValue,Body FROM EmailTemplate WHERE          DeveloperName='Meeting_Reminder'];
        
        Event eve=[select subject,StartDateTime,EndDateTime,Owner.name,Owner.Email,Owner.Phone 
                    From Event where Id=:eventId];
                    
        hbody=template.HtmlValue;
        
        //setting the merge fields
        hbody=hbody.replace('{!Contact.Name}',cname);
        hbody=hbody.replace('{!Event.Subject}',eve.Subject);
         
        plainTxtBody=template.Body;
        plainTxtBody=plainTxtBody.replace('{!Contact.Name}',cname);
        plainTxtBody=plainTxtBody.replace('{!Event.Subject}',eve.Subject);
        
        //Construct the email message
        Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
        mail.setTargetObjectId(cid);
        mail.setSaveAsActivity(false);
        mail.setSubject('Meeting Reminder');
        //mail.setHtmlBody(hbody);
        mail.setPlainTextBody(plainTxtBody);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});     
    }
Also can anyone explain why do we have to set the plaintextbody() even when the email template is of type html?

Any help is appreciated...

Thanks,