Visual Force Email Templates 2

Using custom components makes lots of stuff possible. Here’s an example of a year-end giving thank you email:

screenshot

This shows all gifts for 2008 and attaches a PDF of the giving history as well. Note that this is done just by connecting to the Contact, so this would work in the Salesforce.com mass mail interface.

Update: You cannot use vf templates in mass email or in the apex outboundemail method in Winter ‘09. Maybe next release…

Also note that it’s an ugly HTML email just because I’m not a designer. You can make this as professional as you’re willing to make it.

Here’s the relevant code:

The VisualForce Email Template

<messaging:emailTemplate subject="Thank you for your Support!" recipientType="Contact" >
    <messaging:htmlEmailBody >
        <html>
        <body>
            <p>Hello {!recipient.Household_Greeting__c}--</p>
            <p>Thank you so much for you giving this year. Every gift helps us make a difference in our envrionment...</p>
            <c:thisYearGivingTable ContactId="{!recipient.Id}"/>
            <br/><br/>
            We look forward to seeing you in 2009!
            <br/><br/>
            Best,
            Steve

            </body>
        </html>
    </messaging:htmlEmailBody>
    <messaging:attachment renderas="pdf" filename="{!recipient.Household_Greeting__c}_2008_Giving.pdf">
<html>
<body>
<h3>2008 Giving History for {!recipient.Household_Greeting__c}</h3>
<c:thisYearGivingTable ContactId="{!recipient.Id}"/>

</body>
</html>
</messaging:attachment>
</messaging:emailTemplate>

The VisualForce Component that is included in the Email Template:

<apex:component controller="thisYearGivingTableController" access="global">
	<apex:attribute name="ContactId" description="This is the Contact Id." type="Id" assignTo="{!thisContactId}"/>
	<table border="1">
		<tr>
		    <td><apex:outputText value="Date"/></td>
		    <td><apex:outputText value="Amount"/></td>
		    <td><apex:outputText value="Check Number"/></td>
		    <td><apex:outputText value="Check Date"/></td>
		</tr>
 	<apex:repeat value="{!thisYearOpps}" var="opp" id="theRepeat">
		<tr>
		    <td><apex:outputField value="{!opp.CloseDate}"/></td>
		    <td><apex:outputField value="{!opp.Amount}"/></td>
		    <td><apex:outputField value="{!opp.Check_Number__c}"/></td>
		    <td><apex:outputField value="{!opp.Check_Date__c}"/></td>

		</tr>
	</apex:repeat>
	</table>
</apex:component>

The Apex Controller that powers the logic for the VisualForce Component:

public class thisYearGivingTableController {
	//capture the contact id
	public Id thisContactId {get;set;}
	//a list to hold this year's gifts
	public List<Opportunity> thisYearOpps = new List<Opportunity>();
	//get the gifts into the list
	public List<Opportunity> getThisYearOpps() {
		//criteria for opps
		thisYearOpps = [SELECT Id, Amount,CloseDate, Check_Date__c, Check_Number__c FROM Opportunity
			WHERE IsWon=true AND Year__c=:String.valueOf(system.Today().Year()) AND Id IN
			(SELECT OpportunityId FROM OpportunityContactRole WHERE ContactId = :thisContactId AND Role='Individual Donor')
			ORDER BY CloseDate];
		return thisYearOpps;
	}
}

Kudos to Andrew and the team for making the email template functionality so robust! This is killer stuff!

6 Responses to “Visual Force Email Templates 2”

  1. Jeff Marinchak Says:

    Thanks for sharing your work. It really helps to see how others have tackled these Visualforce challenges. How difficult would it be to add a couple of calculations to your email template? What if you wanted to add the Checks to show a Total Amount? Can you also describe how to do a calculation on the check amounts? Can you also show the percentage that each check represents of the Total Amount?

  2. astro Says:

    Just wanted to thank you for your site, I’ve referenced it numerous times as a ‘budding’ apex/vf developer to overcome some ‘less than obvious’ hurdles.

    Keep up the great work, and keep churnin’ out that great content :)

  3. Steve Says:

    Happy to help out!

  4. Shlomo Ishtov Says:

    I have a template like this including a table. The table can be manually edited (rows deleted, content changed) in Mozilla, but in sales force you cannot delete date or rows. One can only delete the entire table.

    How can I edit the table in IE before sending the email?

    Shlomo

  5. mastram Says:

    Thanks for the great postion.

    Do you know how I can attach files from Opportunity page “Notes and Attachments” section in

    I am want to add files from “Notes and Attachment” section here. Please HELP

  6. mastram Says:

    Thanks for the great postion.

    Do you know how I can attach files from Opportunity page “Notes and Attachments” section in

    messaging:attachment renderas=”pdf” filename=”{!recipient.Household_Greeting__c}_2008_Giving.pdf”
    I am want to add files from “Notes and Attachment” section here. Please HELP

    messaging:attachment

Leave a Reply