0

I want to show the email template of Visualforce type to user with the merge fields. But when I try to fetch the Body or HtmlValue fields from EmailTemplate record it returns null.

Showing markup field value doesn't make sense to user.

How can I get the correct HtmlValue or Body?

1

What I did to resolve this is copying the url of the 'manual' preview and putting that into an Iframe. I tested it for an HTML template but the same principle should work for a VisualForce template I'd think:

<apex:iframe title="Preview" 
             id="previewFrame" 
             scrolling="no" 
             frameborder="0" 
             height="961" 
             width="665" 
            src="{!ContentUrl}/email/templaterenderer?id={!templateId}&related_to_id={!theOpp.Id}&preview_frame=previewFrame&base_href={!BaseUrl}&render_type=REPLACED_HTML_BODY" />

The following APEX variables are used:

public Id templateId {
    get{
        if(templateId == null){
            EmailTemplate template = [select Id from EmailTemplate where DeveloperName = 'The_Email_Template'];
            templateId = template.Id;
        }
        return templateId;
    }
    private set;
}
public String getBaseUrl(){
    return System.URL.getSalesforceBaseUrl().toExternalForm();
}
public String getContentUrl(){
    return System.URL.getSalesforceBaseUrl().toExternalForm().replace('.visual.force.com','.content.force.com');
} 

Lastly, theOpp.Id is in this case the Opportunity Id but can be any Id of the relatedto record for the email template.

  • Hey thanks! I do want the values to be replaced. The merge fields should be there as it is. – ajinkyah Nov 28 '16 at 15:03
  • Aren't they fetched from the related_to_id? As stated, if works for me for normal email templates. So maybe it's a visualforce issue. If you could post the visualforce code (or a sample) I could have a look. – Guy Clairbois Nov 28 '16 at 16:24
  • Currently, I have a dropdown where a list of VF templates is shown. When user selects a template, a remote action is called which returns the subject and body of the selected template. I am using jquery to dynamically inject this email template body in a div on my page as preview. Now, for VF template, it has all those messaging and apex tags which are not relevant to user. So I need something by which these tags are not shown by merge fields are shown as it is. – ajinkyah Nov 29 '16 at 4:52
  • Can you help me on how the HtmlValue and Body field works for different types of email templates? – ajinkyah Nov 29 '16 at 4:53
  • Sorry for the delay. Your mechanism of creating a preview is really very different from what I did, so I'm afraid I can't help you with that. I did notice that when I load a preview of a VisualForce email template with my mechanism, it does fill the merge fields if I provide the related record Id in the url. – Guy Clairbois Dec 1 '16 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.