2

I have Query Regarding Simple Text Email Template.Here I have VF Page And Controller Class in that VF page I want to load Predefined Text Email Template....

Here is the VF Page Code....

<apex:page controller="EmailTemplateTestController">

<apex:form >
    <apex:actionFunction name="send" action="{!send}" rerender="bodyField">
          <apex:param name="c" value="" assignTo="{!emailBodyText}"/>
    </apex:actionFunction>

    <apex:pageBlock title="Compose Email" id="MainPage">
        <apex:pageBlockSection columns="1">

            <apex:pageBlockSectionItem >
            <apex:outputLabel >To</apex:outputLabel>    
            <apex:inputText title="sending address" value="{!ToList}" id="ToField" styleClass="ToField" style="height:16px; width:200px;"/>
            </apex:pageBlockSectionItem>

            <apex:pageBlockSectionItem >
                <span>Body</span>
                <apex:inputTextarea value="{!templateBody}" style="width:400px ; height:400px"/>
            </apex:pageBlockSectionItem>   
            <apex:pageBlockSectionItem >
                <apex:commandButton value="Send" action="{!send}"/>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

And Controller Class Regarding VF Page....

public class EmailTemplateTestController{

public string ToField{get;set;}
public string ToList{get;set;}
public string ccList{get;set;}
public string emailBodyText{get;set;}
public string emailSubject{get;set;}
public EmailTemplate Templates{get;set;}
public String templateBody {get;set;}        
public string Community_Name{get;set;}

public EmailTemplateTestController()
{
     emailBodyText = '';
     Community_Name = 'customer';
     Templates = [Select Id, Name, IsActive,body,Folder.Name From EmailTemplate Where Name = 'VFEmailTemp' LIMIT 1];
     templateBody = Templates.body ;

}

public PageReference send()
{

  List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
  List<string> ToAddresses = ToList.split(',');

  system.debug('-----ToAddresses --'+ToAddresses );

  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  mail.setToAddresses(ToAddresses); 
  mail.setTargetObjectId('00590000001IIC3');             //  User ID

  mail.setTemplateId(Templates.Id);
  mail.setHtmlBody(templateBody );
  mail.saveAsActivity = false;
  //mail.setHtmlBody(templateBody);
  lstMsgs.add(mail);

 // system.debug('-----lstMsgs--'+lstMsgs);

  try{
       Messaging.sendEmail(lstMsgs);
  }
  catch(Exception e)
  {
     system.debug('exception--'+e);
  }

  return null;

}

}

Here I want to Display In Template...

 Hi Amezon113(user FirstName)

For that here i made template and id was used in the controller,here is the snapshot...

enter image description here

But in the output of VF page it displayed same as Binding in Template.......look output

enter image description here

Here I want to Display User's FirstName so what can i do for that,Please Any one Help Me to solve this query regarding Email Template....

  • Hello It's Urgent....Please Help me to find solution of above mention Email Template in VF Page..... – Vivek113 Mar 20 '14 at 11:09
  • Hello Please Help me,I can't find the solutions......Please...... – Vivek113 Mar 21 '14 at 5:07
3

First of all let's have a quick look at the documentation of Messaging.SingleEmailMessage that explains the problem with your approach. Basically when constructing an email you can use either setHtmlBody or setTemplateId, never both. As you can see in your solution, template is not evaluated, because it's covered by the custom message on setHtmlBody. Salesforce's templating engine is provided only for static Email Templates that are provided by setTemplateId, thus you cannot use dynamic templates out of the box.

To make the answer constructive, let me suggest 3 approaches that you could follow to achieve dynamicity of emails:

  1. The mechanism of Visualforce Email Templates is quite powerful. In fact it even allows to attach an APEX Controller to a template. It's a way to go, if you require to customize only certain parts of a message right before sending - for example to add additional notes or add complex logic for some gaps.

  2. More generic approach is implementing own templating engine in APEX. Potentially you could use String.replace method to fill in gaps of a dynamic template with the following simple pattern:

    String customTemplate = 'Hello {!User.FirstName} {!User.LastName}!';
    for(String fieldName : new String[]{ 'FirstName', 'LastName'}) {
        String fieldValue = String.valueOf(anUser.get(fieldName));
        customTemplate.replace('{!User.' + fieldName + '}', fieldValue);
    }
    
  3. Lastly you could use my simple library Salesforce APEX Templates fills in the gaps for you, what gives flexibility near to the original Email Templates:

    String emailBody = new Template('Hello {!User.FirstName} {!User.LastName}!')
        .evaluate(anUser);
    
| improve this answer | |

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.