[SalesForce] VisualForce page to allow inline editing of EmailTemplate HTML

Thanks for taking a look at my problem.

I am building a VisualForce page to email all CampaignMembers associated with a Campaign record. Everything is working except for one little thing:

I have an tag that is rendered when the user clicks "Edit Template". The idea is that the user can preview the email that is going to be sent, which I do by displaying the EmailTemplate.HtmlValue with .

Here's my relevant code:

<!--VisualForce Page -->
<apex:pageBlockSectionItem rendered="{!editMode}">
    <apex:outputLabel value="Edit Template" />
    <apex:inputTextarea id="editTemplate" value="{!strMessage}" richText="true" cols="100" rows="15"/>
</apex:pageBlockSectionItem>

/* Controller */
editedTemplate.HtmlValue = strMessage;
update editedTemplate;

So the problem I am getting, when using the rich-text editor, is the following exception:

FIELD_INTEGRITY_EXCEPTION, The HTML Email Content contains improperly
formatted merge fields. Please check the format of all the merge
fields to ensure they start with {! and end with }.: [HtmlValue]

Although I can get it to work with the plain-text editor. However, it is displayed like this:

<table height="400" width="550" cellpadding="5" border="0" cellspacing="5" >
<tr height="400" valign="top" >
<td style=" color:#000000; font-size:12pt; background-color:#FFFFFF; font-family:arial; bLabel:main; bEditID:r3st1;" tEditID="c1r1" locked="0" aEditID="c1r1" >
<![CDATA[<font face="arial"><font size="3">Dear&nbsp;</font>{!Contact.Salutation}&nbsp;{!Contact.LastName},</font><div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt;"><br></div><div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt;">How are you today? I enjoyed our meeting, let's meet again soon!</div><div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt;"><br></div><div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt;">Sincerely,</div><div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt;"><br></div><div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt;"><font face="arial"><b>{!User.Name}</b></font></div><div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt;"><font face="arial">{!User.CompanyName}</font></div><div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt;"><font face="arial">{!User.Phone}</font></div><div><font face="arial">{!User.Email}</font></div><div style="color: rgb(0, 0, 0); font-family: arial; font-size: 12pt;"><font face="arial"><br></font></div>]]></td>
</tr>
</table>

Unformatted HTML is not appropriate for displaying to the end-user.

Any ideas why the rich-text editor would cause the exception above? What can I do to fix it?

Best Answer

I tried to complete your code to achieve what you describe and found a little problem: sending the email will create a Task object, which conflicts with saving an EmailTemplate (you get a MIXED_DML_OPERATION).

What I ended up doing is creating separate buttons for the save and the email. I used a TemporalTemplate to store the modified version so I can reuse it.

You may be able to avoid the MIXED_DML_OPERATION by using a @Future method to send the email after the save.

Here's my code:

<apex:page controller="EmailTemplateController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton action="{!sendMail}" value="Send Email"/>
<apex:commandButton action="{!saveTemplate}" value="Save Template"/>
</apex:pageBlockButtons>
<apex:pageBlockSection columns="1">
<apex:pageBlockSectionItem >
    <apex:outputLabel value="Target Contact" />
    <apex:inputField value="{!targetContact.ReportsToId}"/>    
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
    <apex:outputLabel value="Edit Template" />
    <apex:inputTextarea id="editTemplate" value="{!editedTemplate}" richText="true" cols="100" rows="15"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

And the controller:

public with sharing class EmailTemplateController {
    public Contact targetContact {get;set;}
    public String editedTemplate {get;set;}
    private String templateId = null;
    public EmailTemplateController() {
        targetContact = new Contact();
        if (ApexPages.currentPage().getParameters().containsKey('template')) {
            templateId = ApexPages.currentPage().getParameters().get('template');
            this.editedTemplate = [select HtmlValue from EmailTemplate where Id = :templateId].HtmlValue;
        }
    }

    public PageReference saveTemplate() {
        EmailTemplate template =  [select Id, HtmlValue from EmailTemplate where Name = 'TemporalTemplate'];
        template.HtmlValue = editedTemplate;
        update template;

        return null;
    }

    public PageReference sendMail() {
        EmailTemplate template =  [select Id, HtmlValue from EmailTemplate where Name = 'TemporalTemplate'];

        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setSenderDisplayName('Tester');
        email.setTemplateId(template.Id);
        email.setTargetObjectId(targetContact.ReportsToId);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        return null;
    }
}
Related Topic