Salesforce provides email templates for sending an email from Salesforce to Contacts, User and Lead etc. There are three types of email templates:
- Text Template
- HTML Template.
- Custom (Without using Letterhead)
- Visualforce Template
Let’s explore Visualforce email template and go over its use cases. Visualforce email template allows developer to access and show the relational data like ( Parent to Child and Vice-Versa) in rows and column format. We can also include a Visualforce Component in a Visualforce email template which allows us to perform some actions before showing the data.
How we can add attachments in Visualforce email template ?
Developer might need to attach the documents while sending an email using Visualforce email template. Salesforce allows to add attachments in visualforce email template by using getContentAsPDF() method of PageReference class. This method was not available in asynchronous call before Winter’16. Now, this method is treated as callout in apex.
Apex Code:
public PageReference sendAttachment() {
PageReference pdf = Page.AccountPage;
//Setting AccountId to the id parameter
pdf.getParameters().put(‘id’,accountId);
// content of attachment
Blob body;
// returns the output of the page as a PDF
body = pdf.getContentAsPDF();
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setContentType(‘application/pdf’);
attach.setFileName(‘accAttahcment.pdf’);
attach.setInline(false);
attach.Body = body;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(new String[] { ‘test@test.com’ });
mail.setSubject(‘Account Details’);
mail.setHtmlBody(‘Please find the attachment’);
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });
// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
Use Custom Controllers with Visualforce Email Template
Developer needs to perform some operation on data before sending them via email. We can add controller to the Visualforce component to perform business logic and this component will work as a normal visualforce page. Developer must to add the component on the Visualforce template using .
For e.g. We want to show the list of Contacts for an Account in a Visualforce template with rows and column and one column should show the name of related Account per contact.This example will show accessing data from Parent to Child.
It requires controller because we need to fetch the name of Account from Contact.(Child to Parent).
- We need to create a component which contains the UI part of the template:
- We need to create controller which handles SOQL and relationship query to show the data on component.
- Visualforce email template holds the component markup.
Visualforce Component:-
<apex:attribute name=
"accId"
assignTo = "{!accId}"type=
"String"
required=
"true"
/>
Controller:
public class AcountController { private final List cons; public Id accId {get;set;} public AcountController() { cons = [ Select Id,FirstName, LastName, Email, Phone From Contact Where AccountId =: accId ]; } public List getContacts() { return cons; } }
Visualforce Template:
Author:
![]() |
AJOMON JOSEPH Senior Salesforce Architect ![]() ![]() ![]() ![]() |