+ Start a Discussion

How to pass custom record Id to Component used in a VF Email template?

I know how to pass record Id to Apex:component from a VF pages using attribute.  I have been struggling for 2 days now in trying to achieve the same with a VF Email template. Thanks in advance for your help. Jerome
The controller:
public class CNPerUpdNdFCon{
public Id perId = ApexPages.currentPage().getParameters().get('Id');
public Person__c per {get;set;}
public CNPerUpdNdFCon(){
per = [select Id, name, NameFirst__c from Person__c where id =: perId];
public VOID updPer(){
update per;
List<messaging.SingleEmailMessage> SingleEmailMessageList = new messaging.SingleEmailMessage[1];
SingleEmailMessageList[0] = new Messaging.SingleEmailMessage();
SingleEmailMessageList[0].setTemplateID('00XF0000001KurK');        //HARD CODED.
SingleEmailMessageList[0].setTargetObjectId('003F00000150NyX');   //jmaison92@gmail.com  Goal is just to send the Email to a contact
Messaging.sendEmail(SingleEmailMessageList, false);
The VF Page. After I click Update, the Email is indeed send to jmaison92@gmail BUT how do I get the per.NameFirst__c to be passed to the custom component?
<apex:page controller="CNPerUpdNdFCon" showHeader="false" sidebar="false">
<!--    https://c.na10.visual.force.com/apex/cnperupdndf?id=a02F00000055p2C   -->
<apex:form >
<apex:inputField value="{!Per.NameFirst__c}" />
<apex:commandButton value="Update" action="{!updPer}" />
The Component:
<apex:component access="global">
<apex:attribute name="NdF" description="FirstName" type="String"/>
Nom de famille in Component:{!NdF}
The VF Email Template: 
<messaging:emailTemplate recipientType="Contact"  subject="We updated your First Name" >
<messaging:htmlEmailBody >
The Contact this Email is sent to is:{!recipient.name}<br/>
Your New First Name is <c:CNPerNdF /><br/>
Your would know it if I knew how to populate NdF="{!????}" in Custom Component Above.

Again it is all here: How to I populate the attribute in the component to get the per.FirtsName__c   ?
Thanks in advance for your help,
Hi Jerome,

There's a number of ways to do this.  Here's a couple.

You could have an action method in the controller that returns a pagereference to the pdf page, and set the id into the parameters. E.g.

PageReference gotoPDF() { PageReference result=Page.MyPdfPage; result.getParameters().put(id, MyObject__c.id); result.setRedirect(true); return result; }

Or you could do something with the onclick event of the button:

<apex:commandButton value="Print" onClick="window.location='MyPdfPage?id={!MyObject__c.id}'; return false;" />


Also see the blog below with additional information and code,


Thank you Ashish. I found a solution by using the WhatIds parameter of the Single Mail method, coupled with the RelaedTo in the Component. It does work, but it took me a lot of time to figurethis one out.  Could you help me with my latest question please. I am stuck. Thanks again, Jerome