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
Scott Walker 73Scott Walker 73 

Help displaying the HTML code saved in a long text field within a classic email template

Hi all

I am trying to work out a way to pull HTML code that gets saved in a long text record and use it to render a classic email template when that record is triggered to be used. If I paste the code in directly into the classic email template it works great but when called on as a field it doesn't work and displays as the raw HTML text. Does anyone have a way in which I could call on the HTML code saved in the field and use it to generate the email?
Best Answer chosen by Scott Walker 73
Vishwajeet kumarVishwajeet kumar
Hello,
i think you need to create Visualforce Email template to render html code, use apex:outputText tag for field which has html code data.

Example : 

<messaging:emailTemplate subject="Welcome" recipientType="Contact" relatedToType="Account">
<messaging:plainTextEmailBody >
Congratulations!
This is your new Visualforce Email Template.
</messaging:plainTextEmailBody>

<messaging:htmlEmailBody >
<html>
      <body>         
      <apex:outputText value="{!relatedTo.description}" escape="false"/>
     </body>
</html>



Thanks

All Answers

Vishwajeet kumarVishwajeet kumar
Hello,
Is long text field type is rich text or text ? Rich text strips some of htmls chars which is not supported by it. please validate if queried html text field data is getting assigned to "html body" of the email. 

I would recommand to store this html code in an actual html email template if Html email structure is not dynamic in structure(not data) by record and use it. You can perform better editing or formatting on html email body if it has been stored in an actual html email template.

Thanks
Scott Walker 73Scott Walker 73
Hi Vishwajeet, thanks for your input here

I have now made the field long text as that seems to make sense if the rich text strips some of the html characters. 

Unfortunately the html code changes for each record so need to show it for each one every time from the record. I do not need to edit any of the html as thats done for me and all works great but can't display it when I call on it as a field within the classic email template. Any ideas how to make the code render correctly within a classic email template would be hugely helpful.
Vishwajeet kumarVishwajeet kumar
Hello,
i think you need to create Visualforce Email template to render html code, use apex:outputText tag for field which has html code data.

Example : 

<messaging:emailTemplate subject="Welcome" recipientType="Contact" relatedToType="Account">
<messaging:plainTextEmailBody >
Congratulations!
This is your new Visualforce Email Template.
</messaging:plainTextEmailBody>

<messaging:htmlEmailBody >
<html>
      <body>         
      <apex:outputText value="{!relatedTo.description}" escape="false"/>
     </body>
</html>



Thanks
This was selected as the best answer
Scott Walker 73Scott Walker 73
Vishwajeet thanks so much!

Just tried this and after a bit of trail and error it has just worked a charm, really appreciate your help with this. Totally fixed our issue!