You need to sign in to do that
Don't have an account?

Visualforce Email template and Custom controller help
Hi,
I'm trying to use a custom controller with a Visualforce Email Template. My problem is pretty simple I think. I'm taking in a value from the custom component and I want to use it in the constructor of the controller. I think I'm only overlooking one tiny thing to make sure everything executes in order, but it's eluding me.
the visualforce email template:
<messaging:emailTemplate recipientType="User" relatedToType="DAM_Digital_Asset__c" subject="Please Approve This"> <!-- <apex:page controller="DAM_approvalsEmailController"> --> <!-- https://c.na7.visual.force.com/apex/testmail?id=00A00000064bh3 --> <messaging:htmlEmailBody > <c:DAM_approvalsEmail DA_Id="{!RelatedTo.Id}" /> </messaging:htmlEmailBody> </messaging:emailTemplate>
the component
<apex:component controller="DAM_approvalsEmailController" access="global"> <apex:attribute name="DA_Id" type="String" description="the Digital Asset ID" assignTo="{!DA_Id}" />
the controller
public class DAM_approvalsEmailController { public DAM_Digital_Asset__c da {get; set;} public String DA_Id {get; set;} public DAM_approvalsEmailController() { this.da = [select id, Name, Process_Step__c, Location__c, Host_Domain__c from DAM_Digital_Asset__c where id = :DA_Id]; }
What am I not doing correctly to get DA_Id from the component so that it can be constructed?
Thanks!!
The component constructor is called before the attribute is assigned to. Move your query out of your constructor and into your getter
True, you are trying to use "DA_Id" in the constructor before the value from the attribute is assign to it.
Moving the query out of the constructor, and create function to perform the query and store the result into a variable.
Since get* functions are called multiple time, it would not be a good idea to perform query every time.