VISUALFORCE EMAIL TEMPLATE WITH COMPONENT TO RENDER ATTACHMENT
Today I am going to share my experience where I had to send email with dynamic attachments. I knew that we could do this easily via Apex code but it is hard to maintain if we have to edit some text or add some value in the template.
Unfortunately we cannot use standard apex:pageblock and apex:pageblocktable tags in Visualforce to generate attachments, so we need to do some modifications to move these over to generic HTML entities. Additionally, the Visualforce email handler doesn’t know how to use the object name.
To make it so we can send emails with the attachment as well as be able to generate the attachment we will be using Visualforce components and embed it in the Visualforce email template tag. Below is the html based visualforce component-
To use this component we will need a Visualforce page which will render it as pdf as below-
Now reference this page in our email template as below-
After saving this, we can test it by clicking the “Send Test and Verify Merge Field” button and choosing a User and an account.
Below is how our VF Template looks like-
AUTOMATICALLY SENDING THE ACCOUNT DETAILS VIA WORKFLOW RULE
Now that we have our Visualforce email done, let’s set up the automation to send it when the status is set to Hot.
PDF attachment from automated Workflow
After updating the status on an existing Account to hot, I get the email below with the beautiful PDF attachment as expected-
Attached is an PDF version for the Account force.com with Opportunity and Contact details. Please review.