A blog about Salesforce CRM Configuration and development

Friday, 11 January 2019

How to attach visualforce component as an attachment ( pdf ) in Visualforce email template.

Hello Guys,

In this blog post, I am going to demonstrate a use case of Visualforce email template.

Why Visualforce template? - The Visulforce template is useful when need to do the advanced operation on data that is sent via email to the recipient. The Visualforce email template uses standard visualforce component. We can also include our custom Visualforce component in the Visualforce email template that makes this template more advanced and dynamic.

The Visualforce email template contained in the <messaging:emailTemplate> tag. The <messaging:emailTemplate> tag must contains either a single <messaging:htmlEmailBody> tag or a single <messaging:plainTextEmailBody> tag.

Many standard visualforce components cannot be used in the <messaging:emailTemplate> like <apex:detail>,<apex:pageBlock>, all input component, etc.

Scenario: Send an email to the client with attachment (pdf) as related list record detail. 

Here we can use <messaging:attachment> tag to attach the contents inside this tag, renderAs attribute responsible for rendering the content as pdf and filename is an attribute used to set attached file name in the email message.  
<c:ContractAttachment ContractId="{!relatedTo.Contracts[0].Id}"/>  
The above line is the custom visualforce component which is used here to show related list data which is going to be sent as an attachment in the visualforce email template. 

Visualforce Email Template:
 <messaging:emailTemplate subject="Testing VF template" recipientType="User" relatedToType="Account">  
   <messaging:attachment renderAs="pdf" filename="Contract" >     
     <c:ContractAttachment ContractId="{!relatedTo.Contracts[0].Id}"/>  
   </messaging:attachment>  
    <messaging:htmlEmailBody >  
       <html>  
         <body>  
           <p style="color:red;">Dear {!recipient.name},</p>  
           <p>Please find attached doc.</p>  
         </body>  
       </html>  
     </messaging:htmlEmailBody>  
 </messaging:emailTemplate>  
ContractAttachment visualforce component.
 <apex:component controller="ContractAttachmentCntrl" access="global">  
   <apex:attribute name="ContractId" description="This is the value for the component." type="String" assignTo="{!contId}" />  
   <html>  
     <head>  
     </head>  
     <body>  
       <table style="border-collapse: collapse; ">  
         <apex:repeat value="{!wrapperList}" var="row">  
           <tr style="border: 1px solid rgb(221, 219, 218); width:700px;">   
             <apex:repeat value="{!row.values}" var="value">  
               <td style="border: 1px solid rgb(221, 219, 218); padding: 15px; ">{!value}</td>  
               <td style="border: 1px solid rgb(221, 219, 218); padding: 15px;">  
                 {!row.values[value]}  
               </td>  
             </apex:repeat>  
           </tr>  
         </apex:repeat>  
       </table>  
     </body>  
   </html>  
 </apex:component>  
Apex Controller: ContractAttachmentCntrl
 public class ContractAttachmentCntrl {  
   //WrapperList  
   public List<Wrapper> wrapperList{get;set;}  
   public string contId{get;set{  
     contId = value;  
     wrapperList = new List<Wrapper>();  
     // getting Contract record Id from page URL  
     if(contId !=null){  
       Contract cont = [select id,Name,BillingAddress,Account.Name,CompanySignedId,CompanySignedDate,EndDate,ContractNumber,StartDate,  
                ContractTerm,CustomerSignedDate, CustomerSignedTitle,Description,ShippingAddress,SpecialTerms,  
                Status from Contract where id=:contId] ;  
       for (Integer idx=0; idx<15; idx++)  
       {  
         wrapperList.add(new Wrapper());  
       }  
       if(cont.Name!=null){  
         wrapperList[0].addValue('Name',cont.Name);  
       }  
       else{  
         wrapperList[0].addValue('Name','N/A');  
       }  
       if(cont.BillingAddress!=null){  
         Address addr = cont.BillingAddress;  
         wrapperList[1].addValue('Billing Street',addr.getStreet());  
       }else{  
         wrapperList[1].addValue('Billing Street','N/A');  
       }  
       if(Account.Name!=null ){  
         wrapperList[2].addValue('Name',cont.Account.Name);  
       }else{  
         wrapperList[2].addValue('Name','N/A');  
       }  
       if(cont.CompanySignedId !=null){  
         wrapperList[3].addValue('Company Signed Id',cont.CompanySignedId);  
       }else{  
         wrapperList[3].addValue('Company Signed Id','N/A');  
       }  
       if(cont.CompanySignedDate !=null){  
         wrapperList[4].addValue('Company SignedDate',String.valueOf(cont.CompanySignedDate));  
       }else{  
         wrapperList[4].addValue('Company SignedDate','N/A');  
       }  
       if(cont.EndDate !=null){  
         wrapperList[5].addValue('EndDate',String.valueOf(cont.EndDate));  
       }else{  
         wrapperList[5].addValue('EndDate','N/A');  
       }  
       if(cont.ContractNumber !=null){  
         wrapperList[6].addValue('Contract Number',String.valueOf(cont.ContractNumber));  
       }else{  
         wrapperList[6].addValue('Contract Number','N/A');  
       }  
       if(cont.StartDate !=null){  
         wrapperList[7].addValue('StartDate',String.valueOf(cont.StartDate));  
       }else{  
         wrapperList[7].addValue('StartDate','N/A');  
       }  
       if(cont.ContractTerm !=null){  
         wrapperList[8].addValue('ContractTerm',String.valueOf(cont.ContractTerm));  
       }else{  
         wrapperList[8].addValue('ContractTerm','N/A');  
       }  
       if(cont.CustomerSignedDate !=null){  
         wrapperList[9].addValue('CustomerSignedDate',String.valueOf(cont.CustomerSignedDate));  
       }else{  
         wrapperList[9].addValue('CustomerSignedDate','N/A');  
       }  
       if(cont.CustomerSignedTitle !=null){  
         wrapperList[10].addValue('CustomerSignedTitle',String.valueOf(cont.CustomerSignedTitle));  
       }else{  
         wrapperList[10].addValue('CustomerSignedTitle','N/A');  
       }  
       if(cont.Description !=null){  
         wrapperList[11].addValue('Description',String.valueOf(cont.Description));  
       }else{  
         wrapperList[11].addValue('Description','N/A');  
       }  
       if(cont.ShippingAddress !=null){  
         Address addr = cont.ShippingAddress;  
         wrapperList[12].addValue('ShippingAddress',String.valueOf(addr.getStreet()));  
       }else{  
         wrapperList[12].addValue('ShippingAddress','N/A');  
       }  
       if(cont.SpecialTerms !=null){  
         wrapperList[13].addValue('SpecialTerms',String.valueOf(cont.SpecialTerms));  
       }else{  
         wrapperList[13].addValue('SpecialTerms','N/A');  
       }  
       if(cont.Status !=null){  
         wrapperList[14].addValue('Status',String.valueOf(cont.Status));  
       }else{  
         wrapperList[14].addValue('Status','N/A');  
       }  
     }  
   }}  
   public void setcontId(string value){  
     system.debug('value: ' +value);  
     contId = value;  
   }  
   //Constructor  
   public ContractAttachmentCntrl(){  
   }  
   //Wrapper class  
   public class Wrapper{  
     public Map<String,string> values {get; set;}  
     // constructor  
     public Wrapper()  
     {  
       values = new Map<String,String>();  
     }  
     //Wrapper method  
     public void addValue(String Name, String Value)  
     { //put data in values map.  
       values.put(Name,Value);  
     }  
   }  
 }  
Share:

0 comments:

Post a comment

Trailhead Profile


Follow by Email

Total Pageviews

Followers

Popular Posts

Powered by Blogger.

Contact form

Name

Email *

Message *