Not able to call a variable from constructor












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.










share|improve this question



























    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.










    share|improve this question

























      3












      3








      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.










      share|improve this question













      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.







      apex visualforce visualforce-component email-template






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 29 at 7:36









      Umair Ayaz

      161




      161






















          1 Answer
          1






          active

          oldest

          votes


















          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;
          }
          }





          share|improve this answer





















          • Thanks Aiden. It worked!
            – Umair Ayaz
            Nov 29 at 10:41










          • 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 at 10:45











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "459"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f240916%2fnot-able-to-call-a-variable-from-constructor%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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;
          }
          }





          share|improve this answer





















          • Thanks Aiden. It worked!
            – Umair Ayaz
            Nov 29 at 10:41










          • 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 at 10:45
















          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;
          }
          }





          share|improve this answer





















          • Thanks Aiden. It worked!
            – Umair Ayaz
            Nov 29 at 10:41










          • 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 at 10:45














          3












          3








          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;
          }
          }





          share|improve this answer












          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;
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 29 at 9:20









          Aidan

          6,795942




          6,795942












          • Thanks Aiden. It worked!
            – Umair Ayaz
            Nov 29 at 10:41










          • 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 at 10:45


















          • Thanks Aiden. It worked!
            – Umair Ayaz
            Nov 29 at 10:41










          • 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 at 10:45
















          Thanks Aiden. It worked!
          – Umair Ayaz
          Nov 29 at 10:41




          Thanks Aiden. It worked!
          – Umair Ayaz
          Nov 29 at 10:41












          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 at 10:45




          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 at 10:45


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Salesforce Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f240916%2fnot-able-to-call-a-variable-from-constructor%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents