3

I am using a visualforce email template which calls a visualforce component and that component calls the controller class.

I am sending an Id from the Email Template to the component and trying to set it to another variable in the controller. It does not print the id from the component when i try to set it in the constructor and print it in the component. It would be great if someone could tell me where am I wrong. I researched on this issue and most likely it looks like There is an execution order that is not met. please help me fix this poblem and print the Id.

Visualforce Template:

<messaging:emailTemplate subject="Account overview Document" recipientType="User" relatedToType="pse__Proj__c">
<messaging:plainTextEmailBody >
<c:Information_Document_component Project_Id="{!relatedTo.Id}"/>
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

Visualforce Component:

<apex:component controller="Information_Document_Controller" access="global" >
<apex:attribute: required="required" name="Project_Id" type="String" description="Test" AssignTo="{!projectId}"/>
{!projectId} <!-- This prints the correct Id -->
{!test} <!-- This prints as "test result: null where as I want this to print the Id too-->
</apex:component>

Controller:

public class Information_Document_Controller {

    public String test {get; set;}
    public String projectId{get; set;} 

    public Information_Document_Controller(){
        test = 'test result: '+projectId;

    }

}

Please note that i have simplified the class just for this issue. Once i am able to get the Id in the constructor i will be using it in queries to return some fields from few of the related objects. Thank You in advance.

1 Answer 1

3

Sadly, that's just how VisualForce components work. They are constructed first, then the parameters are set.

So, typically, you have to implement custom setter methods. Each one calls an initialisation method which you write to check when all of the parameters have been initialised, then so its actual work e.g.

public String test {get; set;}

public String projectId{get; set {
    projectId = value;
    initialise();
}}

private void initialise() {
    if(projectId != null) {
        test = 'test result: '+projectId;
    }
}
1
  • Great! Could you accept the answer so that anyone else with the same problem can see that there is a working answer here?
    – Aidan
    Nov 29, 2018 at 10:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .