function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ron-LONRon-LON 

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!!

 

gtuerkgtuerk

The component constructor is called before the attribute is assigned to. Move your query out of your constructor and into your getter

bmabma

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.

 

 

public class DAM_approvalsEmailController {

 private DAM_Digital_Asset__c da;
 public String DA_Id {get; set;} 
 
 public DAM_approvalsEmailController() {}

 public DAM_Digital_Asset__c getda() {
   if (this.da == null && this.DA_Id != null) {
       this.da = [select id, Name, Process_Step__c, Location__c, Host_Domain__c from DAM_Digital_Asset__c where id = :this.DA_Id]; 
   }

   return this.da;
 }