Sometimes we need to attach dynamic attachment to a Salesforce email template. So, following are the steps to create dynamic attachments for an email template.
Create a Visualforce Page and an apex controller to generate a PDF document.
Create a Visualforce Page Component and an apex controller for Visualforce Email Template.
To show some records or render customized content on a visualforce email template, we need to include a custom component in a Visualforce email template that uses the custom controller.
Here is an example of visualforce email template with list of contacts of one account record.
Apex Controller:
public class AccountEmailTemplate
{
public Id accountId {get;set;}
public List<Contact> getContactList()
{
List<Contact> conList;
conList = [SELECT FirstName, LastName, Email, Phone FROM Contact WHERE AccountId =: accountId];
return conList;
}
}
<messaging:emailTemplate subject="List of Contacts" recipientType="User" relatedToType="Account">
<messaging:htmlEmailBody >
Hi,<br/>
Below is the list of contacts for account {!relatedTo.Name}.<br/><br/>
<!--Embedded Visualforce component here -->
<c:ContactList AccId="{!relatedTo.Id}" /><br/><br/>
<b>Regards,</b><br/>
{!recipient.FirstName}
</messaging:htmlEmailBody>
</messaging:emailTemplate>
Output:
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject