Wednesday, May 5, 2010

Visualforce Email Template with Attachment

Visualforce Email Templates with Component as Attachment

Now Email templates can be created through a visualforce. That is you can send emails to people with salesforce features.

I will explain a Visualforce email template basic/as well as with component as attachement here.

<messaging:emailTemplate recipientType="Contact" relatedToType="Account" subject="opportunity report for Account : {!relatedTo.name}">
<messaging:htmlEmailBody >
<html>
<body>
<table >
    <tr>
        <th> Name </th>
        <th> CloseDate </th>
        <th> Stage Name </th>
    </tr>
    <apex:repeat var="o" value="{!relatedTo.Opportunities}">
        <tr>
           <td>{!o.Name}</td>
           <td>{!o.CloseDate}</td>
           <td>{!o.StageName}</td>
        </tr>
    </apex:repeat>    
</table>
   
</body>
</html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>
The above Visualforce Template sends out all the opportunities that are related to the Contact's Account.

The one more powerful usage of Visualforce Email Template is We can introduce Custom Components in the body.

I would like to attach a PDF document which shows the number of Opporutnities for that account grouping by their close date. This I can do in a component and include that component inside my email template like this after htmlEmailBody tag.
<messaging:attachment renderAs="PDF">
    <c:opportunityGrouping accountId="{!relatedTo.Id}"/>
</messaging:attachment>
component:
<apex:component controller="opportunityGroupingController" access="global">
    <apex:attribute name="accountId" assignTo="{!accId}" type="String" description="Id of the account"/>
    <table >
            <tr>
                <th> Total </th>
                <th> CloseDate </th>
            </tr>
        <apex:repeat var="opp" value="{!GroupedOpportunites}">
            <tr>
                <td>{!opp.Total}</td>
                <td>{!opp.CloseDate}</td>
            </tr>
        </apex:repeat>    
    </table>
</apex:component>
opportunityGroupingController Class:
public class opportunityGroupingController 
{
    public String accId
    {   get;set;    }
   
    public list<AggregateResult> lstAR = new list<AggregateResult>();
   
    public list<subClass> lstSC = new list<subClass>(); 

    public class subClass
    {
        public Integer Total
        {   get;set;    }

        public date closeDate
        {   get;set;    }
       
        public subClass(AggregateResult ar)
        {
            Total = (Integer)ar.get('Total');
            closeDate = (date)ar.get('CloseDate');              
        }
    }

    public list<subClass> getGroupedOpportunites()
    {  
        lstAR = [select count(Id) Total, CloseDate from Opportunity where AccountId =:accId Group By CloseDate];
        for(Integer i = 0; i < lstAR.size(); i++)
        {
            subClass objSubClass = new subClass(lstAR[i]);
            lstSC.add(objSubClass);
        }
        return lstSC;  
    }
}

Hope this is useful.

Thanks
Srinivas
Technology Evangelist
Trekbin Technologies

7 comments:

  1. Srinivas, do know if I can send a contact a list of all "Open" cases associated with their account? And if so, can I send such an email on a regualar scheduled basis (e.g. every Friday at 9am)
    thanks!
    Paul

    ReplyDelete
  2. Hi

    Help! have an customobject1 and customobject2
    want to send email to the contact in customobject1 and add on all the related entries on customobject2

    just cant figure it out
    happy to pay!

    ReplyDelete
  3. Hello Srinivas

    I have one issue .... I have to include buttons like accept or reject buttons in the email notification ....when click on the accept ....that lead should converted to an opportunity ......Could you please help me out with this

    Thanks in Advance

    ReplyDelete
  4. Hi Srinivas,

    Thank you for this wonderful post. Can you also please include a snippet to show how to call this template from your apex trigger or class??

    Thanks,
    Raghavendra

    ReplyDelete
  5. hi srinivas.
    thank you for wonderful post. plz keep on adding interview questions like this

    ReplyDelete
  6. Good and Nice Explanation. Thanks for sharing! (nara.dotnet@gmail.com)

    ReplyDelete
  7. Can I add merge field or any field value to subject of visualforce email template?
    (khemanshurao@gmail.com)

    ReplyDelete