Monday 20 January 2014

using custom controllers in visualforce email templates

using custom controllers in visualforce email templates

To be able to do some calculation or query some records to show on a visualforce email template we would certainly need a controller associated with VF email template. But a visualforce email template attribute [<messaging:emailTemplate>] does not allow direct association of controller with it. A workaround for this is, embedding a visualforce component that can use  controller with it. We can then use this component in the visualforce email template. 

Lets say, we want to display some randomly chosen accounts in the email template.This is not possible unless we query in the database to retrieve the records, hence we would usevisualforce component to do the task.
For this, we would need to create a visualforce component,its controller and then use the component in the email template as below,

Controller for Visualforce Component

1
2
3
4
5
6
7
Public class accountList{
Public List<account> accList{get;set;}
 Public accountList(){
  accList = new List<account>([select name,id,description,accountnumber,annualrevenue from account limit 5]);
 }
}

Visualforce Component

1
2
3
4
5
6
7
8
9
10
11
12
13
<apex:component controller="AccountList" access="global">
  <apex:datatable value="{!accList}" var="acc">
    <apex:column value="{!acc.name}">
      <apex:facet name="header">Name</apex:facet>
         </apex:column>
         <apex:column value="{!acc.accountnumber}">
           <apex:facet name="header">Account Number</apex:facet>
             </apex:column>
    <apex:column value="{!acc.annualrevenue}">
      <apex:facet name="header">Annual Revenue</apex:facet>
        </apex:column>
   </apex:datatable>
</apex:component>
Visualforce Email template

1
2
3
4
5
6
7
8
<messaging:emailtemplate subject="Product Enquiry" recipienttype="Lead">
  <messaging:htmlemailbody>
     Congratulations!
     This is your new Visualforce Email Template. Following are a list of randomly choosen accounts in my org:
    <!-- Embedding visualforce component here -->
    <c:accountlist>
  </c:accountlist></messaging:htmlemailbody>
</messaging:emailtemplate>
Now when we use this email template it will populate the records from the visualforce component, as you can see in the screen shot below,
Some points that needs to be considered before using a visualforce component in VisualForceemail template:
1. Make sure you specify the access as global for your compoennt, otherwise your email template would not compile and give below error, <messaging:emailTemplate> can only contain components with an access level of global. <c:accountlist> is not valid.
   <apex:component controller="AccountList" access="global">
2. Visualforce email template cannot contain pageblock and its child attributes(pageblocktable for example), also form cannot be included in vf email template, hence you cannot put this in the visualforce compoennt to be used in vf email template, for vf component would be a part ofemail template itself.

No comments:

Post a Comment