Last active
March 21, 2017 04:58
-
-
Save dhoechst/3b3e6a2eae9c5f579c513e4987208214 to your computer and use it in GitHub Desktop.
Dynamic Email Templates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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