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
Tim__mTim__m 

Email Template getHtmlBody()

Using the SingleEmailMessage class I am trying to set the template Id, what Id, and target Id. I was disappointed when I tried to get the html body and found the body to be null. Is there a way to get an email template body after a merge with an sObject? Here is what I thought would work...

 

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(contactId);
mail.setTemplateId(templateId);
mail.setWhatId(caseId);
		
System.debug('*********************');
System.debug(mail.getHtmlBody()); // getHtmlBody returns null :(

 

 

Imran MohammedImran Mohammed

The Body of the template will be retrieved on the fly when you invoke the sendEmail method.

In the code you are just instructing that the SingleEmailMessage will use this particular templateId for sending out emails.

Setting the id will no way fetch the Body of Email.

 

As far as i know fetching the template body after merging with SObject cannot be done.

Tim__mTim__m

Thanks Imran,

 

I'm trying to get some answers from primer support and developer support but after they waste my time they will probable just say the same thing you just said.

EyalEyal

is their any news on this ? can we retrive today the HTML ?


rickarnettrickarnett
You can get the HTML body after invoking the sendEmail method.  If for some reason, you want the body, but you don't want the email sent, you can follow along at this page: http://www.opfocus.com/blog/sending-emails-in-salesforce-to-non-contacts-using-apex/

T
he basic idea is that you set up a save point, call sendEmail, get the rendered text, and then rollback the transaction.  The email won't be sent because the transaction gets rolled back and you have your rendered text.