TransWikia.com

Visualforce button to open EmailTemplate preview in new page causes error

Salesforce Asked on November 30, 2021

I have a Visualforce page where I allow a user to edit text to add to a VF template and then send it. (I can’t use an HTML template as there is an attachment which is built up using data from the selected Order, and one can’t edit a VF template before sending ).

enter image description here
I’ve researched and made some progress, but am now getting the error “Formula Expression is required on the action attributes“. I build up the url in my controller and pass it to the form in an action. If I take the link it creates and just paste it into the address bar, the link works fine, but not if it goes via the button/link. What am I doing wrong?

I tried using both an apexCommandButton and apexCommandLink, but they give the same error (except the button doesn’t open in a new page, but overwrites the current page).

The other thing I don’t know how to do is populate the displayed template (the outputText field) with the data (as a preview – see pic where it just displays the relatedTo. fieldnames). Anyone know how I can do that?

My VF page:

<apex:page standardController="Order" extensions="CreateRMAEmailController" title="RMA Email" showHeader="true" sidebar="true">
<apex:form >
    <apex:actionFunction name="OpenEmail" action="{!CreateRMAEmail}"/>
    <apex:sectionHeader subtitle="Create RMA Email" title="RMAEmail"></apex:sectionHeader>
    <apex:pageMessages ></apex:pageMessages><!-- error messaging section for page - allows for display of any issues to the human -->
    <apex:pageBlock title="Email" id="pb">
        <apex:pageBlockButtons location="top">               
            <apex:commandLink target="_blank" styleClass="btn" style="text-decoration:none;padding:4px;" 
                              action="{!CreateRMAEmail}" value="Send Email link"/>
            <apex:commandButton value="Send Email" onclick="OpenEmail();return false;"/>
            <apex:commandButton value="Cancel" action="{!cancel}" immediate="true"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Custom Message" collapsible="false">
            <apex:inputField id="selrecip" value="{!Order.Select_Email_Recipient__c}" label="Select Recipient" required="false"/>
            <apex:inputField id="recip" value="{!Order.RMA_Email_Recipient__c}" label="Recipient Email" required="false"/>
            <apex:inputField id="notes" value="{!Order.Email_Notes__c}" />
            <apex:outputLabel value="Email Template" for="templ">
                <apex:outputText id="templ" value="{!et.markup}" escape="false"/>
            </apex:outputLabel>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

My controller:

public with sharing class CreateRMAEmailController {

private final Order o { get; set; }
public string emailUrl { get; set; }

public EmailTemplate et {get; set; }

//-----------------------------------------------------------------------------------------
public CreateRMAEmailController(ApexPages.StandardController std) {
    o = (Order)std.getRecord();
    et = [select id, name, markup from EmailTemplate where developername = 'RMA'];
    et.Markup = et.Markup.substringBeforeLast('<!-- --------');
    if(o.Email_Notes__c == null){
        o.Email_Notes__c = 'Dear ';
    }
}


//-----------------------------------------------------------------------------------------
public string CreateRMAEmail(){

    Id templateId = et.Id; 

    emailUrl = '"<a target="_blank" href="' + System.URL.getSalesforceBaseURL().toExternalForm();
    emailUrl += '/_ui/core/email/author/EmailAuthor?rtype=003&p3_lkid=' + o.Id + 
        '&retURL=%2F' + o.Id +
        '&p5=' + UserInfo.getUserEmail() + 
        '&p2_lkid=' + o.CustomerAuthorizedById + 
        '&new_template=true&template_id=' + templateId +
        '&new_template=true';
    emailUrl += '">Send Email</a>';

    system.debug('####CreateRMAEmailController emailUrl: ' + emailUrl);
    return emailUrl;


}

}

One Answer

Typical, I post the question and then find the answer (well, to part 1)... It had to do with quotes around the URL. I still don't know how to populate the the template for the preview though??

    emailUrl = '"' + System.URL.getSalesforceBaseURL().toExternalForm();
    emailUrl += '/_ui/core/email/author/EmailAuthor?rtype=003&p3_lkid=' + o.Id + 
        '&retURL=%2F' + o.Id +
        '&p5=' + UserInfo.getUserEmail() + 
        '&p2_lkid=' + o.CustomerAuthorizedById + 
        '&new_template=true&template_id=' + templateId +
        '&new_template=true"';

Answered by Irene on November 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP