Skip to content

Instantly share code, notes, and snippets.

@dhoechst
Last active March 21, 2017 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhoechst/3b3e6a2eae9c5f579c513e4987208214 to your computer and use it in GitHub Desktop.
Save dhoechst/3b3e6a2eae9c5f579c513e4987208214 to your computer and use it in GitHub Desktop.
Dynamic Email Templates
public class DynamicEmailController {
public DynamicEmailController() {}
public String emailRelatedToId {get; set;}
public String emailTemplateId {get; set;}
public Boolean isHtml {get; set;}
private Messaging.SingleEmailMessage renderedEmail {
get {
if (renderedEmail==null) {
renderedEmail = Messaging.renderStoredEmailTemplate(emailTemplateId, '', emailRelatedToId);
}
return renderedEmail;
}
set;
}
public String getMergedEmailHtmlBody() {
return renderedEmail.getHtmlBody();
}
public String getMergedEmailPlainTextBody() {
return renderedEmail.getPlainTextBody();
}
public String getMergedEmailSubject() {
return renderedEmail.getSubject();
}
}
<apex:component controller="DynamicEmailController" access="global">
<apex:attribute name="RelatedToId" assignTo="{!emailRelatedToId}" type="String" description="Id of the RelatedTo Object for the email message"/>
<apex:attribute name="TemplateId" assignTo="{!emailTemplateId}" type="String" description="The label that contains the template Id" />
<apex:attribute name="HTML" assignTo="{!isHTML}" type="Boolean" description="Set to true if you want HTML" />
<apex:outputText escape="false" value="{!mergedEmailHtmlBody}" rendered="{!isHtml}"/>
<apex:outputText escape="false" value="{!mergedEmailPlainTextBody}" rendered="{!!isHtml}"/>
</apex:component>
<messaging:emailTemplate subject="{!SUBSTITUTE($Label.AccountEmailSubject,'{0}', relatedTo.Name)}"
relatedToType="Account" language="{!relatedTo.Language__c}">
<messaging:htmlEmailBody >
<c:DynamicEmail RelatedToId="{!relatedTo.Id}" TemplateId="{!$Label.AccountEmailId}" HTML="true"/>
</messaging:htmlEmailBody>
<messaging:plainTextEmailBody >
<c:DynamicEmail RelatedToId="{!relatedTo.Id}" TemplateId="{!$Label.AccountEmailId}" HTML="false"/>
</messaging:plainTextEmailBody>
</messaging:emailTemplate>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment