Visualforce Email Template

2:22 AM

Normally we create email templates by navigating to Setup--> Communication Templates --> Email Templates .. Merge fields are used in these templates to display data...


To enable users to create flexible email templates, Visualforce Email templates was introduced.. You can create templates with extensive HTML and Visualforce coding so as to develop some really appealing and effective templates.. But there are some limitations when you create a visualforce email template.. Before going into the limitations let us create a simple template now..

Create a Visualforce Email template by navigating to
Setup --> Communication Templates --> Email Templates --> New Template
Select "Visualforce" in the first step...

Give a tilte,name,Subject for your template. Next select the Receipent type and Related to (Related to denotes the object from which you would want data to be displayed in your Email).. Click "Save".... Remember to check "Available for use" to make your email template available for use in Workflow email alerts and whereever it may be useful....
You will now see the template detail Page... In the "Email template" section click on "Edit Template" button... This will take you to a Visualforce Page editor...

You can also click on "Attach file" button in "Standard Attachments" section to attach a file from your desktop to your email..

Below is a small piece of code... You will have to put this code in the editor that appears when you click on "Edit template" button
Note: I have used "htmlEmailBody" for including HTML tags in my template.. Also, since the "Relatedto" is "Account" i am displaying Account information in my template..
<messaging:emailTemplate subject="Testing Visualforce Email Template" recipientType="User" relatedToType="Account">
<messaging:HtmlEmailBody >
Account Name : {!Relatedto.Name} <br/>
Account Description : {!Relatedto.Description} <br/>
</messaging:htmlEmailBody>
</messaging:emailTemplate>

What you Can...
With Visualforce email templates you can display any data from the related object in any format you wish.. If Relatedtotype =Account you can display any field of the Account and you can also display the related lists of Account .. For ex you can create a datatable with the value="{!Relatedto.Contacts}"... Similarly you can display other related lists...

What you Cannot...
You cannot use the <apex:page> tag inside your Email template.. Having said this you cannot use pageblock, pageblocksection or any other tag which is a child of the <apex:page> tag.. You can use outputfield,outputtext,datatable etc.. Do not attempt too much of styling and formatting for your email templates unless it is extremely necessary because you would'nt know how your email would be rendered by different providers.. Always use simple and standard formatting so that your email displays uniformly across all providers...

11 comments

  1. Hi,
    Thanks a lot for such a nice article.
    Just had a little doubton the relatedtotype object. Does it need to be the Relatedto object of the contact object(in case i am sending mail to contacts) or is it the related object of the object which is sending the mail? is it possible to do data binding with data from the apex controller class in a template?

    Thanks in advance,
    Nilakshi

    ReplyDelete
  2. You could do it, if you put a Visual Force component into the template

    ReplyDelete
  3. can I force a 'recipient'?

    ReplyDelete
  4. I have done the same, but in email my values are not coming

    ReplyDelete
  5. My email fields are empty, they don't show on the email. Any ideas why not?

    ReplyDelete
  6. I have one question - Can I display or attach a document to this template. For example, if a user selects this template from the Opportunity > from Activity History section >> Send An Email button. Then the attachments which are in that Opportunity should be attached to that Email where this template is selected. Can we do this?

    ReplyDelete
  7. I have done the same, but in email my values are not showing in Email

    ReplyDelete
  8. I am receiving blank emails

    ReplyDelete
  9. The emails being sent out from WorkFlow dont contain any data.

    ReplyDelete
  10. Same problem...no data coming from controller when I tried using a component in template

    ReplyDelete