Send Pdf Using Visualforce Email Template

Requirement:  Suppose you have a Object(Invoice__c) Record , There is  isSend Checkbox field , When 
isSend = true then email goes to Admin user with Pdf Attachment. In Pdf there in Object Data and Related Child data in Table.

Solution : For this you have two solution 1) Create a future call out class and write a trigger ,
In future class u can get pdfcontent

2) Using visualforce Email Template

I m explaining Second one.

We need to create Custom Component.

Setup>> Develop>> Component>> New

Component Name: GenrateInvoiceComponent
and Create a Class  GenrateInvoiceComponentController

Step 1: Create a Class GenrateInvoiceComponentController

      GenrateInvoiceComponentController  Class Code Example

public class GenrateInvoiceComponentController
{
    public string InvoiceID{get;set;} // this invoice will come from Url
Public List<InvoiceLineItem> lstofChildRecortd{get;set;}
  

    public string GenrateInvoiceComponentController
    {
        get
        {
            if(InvoiceID != null)
            {
lstofChildRecortd =[Select id, InvoiceID__C,Name,Unit Price, QUANTITY,Amount from InvoiceLineItem where InvoiceID__C =:InvoiceID]
            }
   return ''; 
      }

 set; 

}

Step 2
Component Code: Name:  GenrateInvoiceComponent 

<apex:component controller="GenrateInvoiceComponentController" access="global">   
   <apex:attribute name="strInvoiceId" assignTo="{!InvoiceID}" type="string" description="Id of invoice" />

{!GenrateInvoiceComponentController}  

 <table  width="100%" border="1" cellspacing="0">
      <tr>
        <td>Name/td>
        <td> Unit Price</td>
        <td> QUANTITY</td>
        <td> Amount</td>      
    </tr>     
    <apex:repeat var="objRec" value="{!lstofChildRecortd}"  rendered="{!IF(lstofChildRecortd != null,true,false)}" >
<td>{!objRec.Name}/td>
        <td> {!objRec.Unit Price}</td>
        <td> {!objRec.QUANTITY}</td>
        <td> {!objRec.Amount}</td>    
    </tr>  
    </apex:repeat>   
</table>
</apex:component>

Step 3-4  Now you need to create a VF page and its  controller
VFpage Name:  InvoiceTypePdf
Class  Name:  InvoiceTypePdfController


Step :3

 Create InvoiceTypePdfController Class code


public class InvoiceTypePdfController{


    public string InvoiceID 
    {
        get
        {
            return ApexPages.currentPage().getParameters().get('id');
        }
        set;
    }
    
   
}

Step :4
 Create InvoiceTypePdf VF page code
VF page code:

<apex:page controller="InvoiceTypePdfController" renderAs="pdf">

   // Below is component which one I created
   // "strInvoiceId" this name should be same as attribute name in component above 

     <c:GenrateInvoiceComponent strInvoiceId="{!InvoiceID}">
  </c:GenrateInvoiceComponent>
  
</apex:page>


Step 5 : Need to create VF EMail Template

Setup>> Email Template>> New>> Visualforce Email Template


<messaging:emailTemplate subject="Detail About Invoice" recipientType="Contact" relatedToType="Invoice__c">

<messaging:htmlEmailBody >


Dear Sir/Madam<br/><br/>

Greetings!!<br/><br/><br/>


Annexed please find your order invoice.<br/><br/>

•   <b>Invoice Name </b>- {!relatedto.Name} <br/>

Please feel free for any other assistance.<br/><br/><br/>

Thank You<br/>

</messaging:htmlEmailBody>

<messaging:attachment filename="Invoice.pdf" renderAs="pdf" >
    <c:GenrateInvoiceComponent  strInvoiceId="{!relatedto.id}" >     
  </c:GenrateInvoiceComponent >
</messaging:attachment>

</messaging:emailTemplate>


Step 6: Create a Workflow and Chose condition when On Invoice object isSend == true then Send Email to ADmin user with Selected VF Email Template


Note: In case of any issue please feel free ask to me


Thanks
Sumit Shukla
9711055997
sumitshukla.mca@gmail.com

Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page