Wednesday, May 23, 2012

VisualForce Email Template

Three tags to create VisualForce email template
1) <messaging:emailTemplate> – in which you specify recipient type, relatedToType, subject and email address that they can reply back to.
2) <messaging:htmlEmailBody> – in which you define the html content you want to show in your Email.
3) <messaging:plainTextEmailBody> – in which you include the text version of your Email.
Let me show you a simple example of how to create a VisualForce email template. We are going to create an email template which sends email about contacts that are related to an account.
The first step is to create an email template by going to Setup -> Administration Setup -> Communication Templates -> Email Templates -> New Template


<messaging:emailTemplate subject="Contact Information for Account: {!relatedTo.name}" recipientType="Contact" relatedToType="Account" replyTo="sivateja.s@gmail.com">
<messaging:htmlEmailBody >
<html>
<body>
<p> Dear {!recipient.name},</p>
<p> Below is the list of contacts related to your account: {!relatedTo.name}.</p>
<table border="0">
<tr>
<th> Action </th>
<th> Contact Name </th>
<th> Contact Email </th>
</tr>
<apex:repeat var="con" value="{!relatedTo.Contacts}">
<tr>
<td> <a href="na7.salesforce.com/{con.id}"> View </a>
<a href="na7.salesforce.com/{con.id}/e"> Edit </a> </td>
<td> {!con.Name} </td>
<td> {!con.Email} </td>
</tr>
</apex:repeat>
</table>
</body>
</html>
</messaging:htmlEmailBody>
<messaging:plainTextEmailBody >
Dear {!recipient.name},

Below is the list of Contacts related to Account: {!relatedTo.name}.

[Contact Name] - [Contact Email]
<apex:repeat var="cont" value="{!relatedTo.Contacts">
[cont.Name]  -  [cont.Email]
</apex:repeat>

For more detailed information login to http://www.salesforce.com
</messaging:plainTextEmailBody>
</messaging:emailTemplate>
 
Last step is to test and verify merge fields. Click Send Test and Verify Merge Fields button to verify merge fields.

No comments:

Post a Comment