1

So we have a Visualforce page:

<apex:page controller="Problem_GenerateDynamicComponent">
    <c:Problem_Demonstration_1 />
    <apex:dynamicComponent componentValue="{!component}" />
</apex:page>

If we run the page like this, and we check the debug logs, we see that the constructor of the Page is run before the constructor of the Components:

(Check the "Constructor of Component" and "Constructor of Page" debug messages that we put in the constructors)

34.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
16:07:05.044 (44837727)|EXECUTION_STARTED
16:07:05.044 (44873673)|CODE_UNIT_STARTED|[EXTERNAL]|066250000000Je9|VF: /apex/Problem_Demonstration_1
16:07:05.046 (46481589)|CODE_UNIT_STARTED|[EXTERNAL]|01p250000009Nus|Problem_GenerateDynamicComponent <init>
16:07:05.046 (46498858)|SYSTEM_MODE_ENTER|true
16:07:05.047 (47335826)|METHOD_ENTRY|[1]|01p250000009Nus|Problem_GenerateDynamicComponent.Problem_GenerateDynamicComponent()
16:07:05.047 (47349714)|METHOD_EXIT|[1]|Problem_GenerateDynamicComponent
16:07:05.047 (47497434)|SYSTEM_METHOD_ENTRY|[6]|System.debug(ANY)
16:07:05.047 (47528424)|USER_DEBUG|[6]|DEBUG|Constructor of Page : Problem_GenerateDynamicComponent
16:07:05.047 (47537994)|SYSTEM_METHOD_EXIT|[6]|System.debug(ANY)
16:07:05.049 (49728813)|SYSTEM_METHOD_ENTRY|[7]|Problem_GenerateDynamicComponent.__sfdc_component(ApexPages.Component)
16:07:05.049 (49762475)|SYSTEM_METHOD_EXIT|[7]|Problem_GenerateDynamicComponent.__sfdc_component(ApexPages.Component)
16:07:05.049 (49778474)|CODE_UNIT_FINISHED|Problem_GenerateDynamicComponent <init>
16:07:05.049 (49829623)|CODE_UNIT_STARTED|[EXTERNAL]|01p250000009Nus|Problem_GenerateDynamicComponent get(component)
16:07:05.049 (49838217)|SYSTEM_MODE_ENTER|true
16:07:05.049 (49856896)|CODE_UNIT_STARTED|[EXTERNAL]|01p250000009Nus|component
16:07:05.049 (49869087)|CODE_UNIT_FINISHED|component
16:07:05.049 (49881381)|CODE_UNIT_FINISHED|Problem_GenerateDynamicComponent get(component)
16:07:05.050 (50295776)|CODE_UNIT_STARTED|[EXTERNAL]|01p250000009Ntp|Problem_Demonstration_1 <init>
16:07:05.050 (50308991)|SYSTEM_MODE_ENTER|true
16:07:05.050 (50350745)|METHOD_ENTRY|[1]|01p250000009Ntp|Problem_Demonstration_1.Problem_Demonstration_1()
16:07:05.050 (50360387)|METHOD_EXIT|[1]|Problem_Demonstration_1
16:07:05.050 (50401736)|SYSTEM_METHOD_ENTRY|[22]|System.debug(ANY)
16:07:05.050 (50418267)|USER_DEBUG|[22]|DEBUG|Constructor of Component : Problem_Demonstration_1
16:07:05.050 (50425063)|SYSTEM_METHOD_EXIT|[22]|System.debug(ANY)
16:07:05.050 (50619618)|SYSTEM_METHOD_ENTRY|[24]|Problem_Demonstration_1.__sfdc_someAccount(Account)
16:07:05.050 (50650265)|SYSTEM_METHOD_EXIT|[24]|Problem_Demonstration_1.__sfdc_someAccount(Account)
16:07:05.050 (50756398)|SYSTEM_CONSTRUCTOR_ENTRY|[28]|<init>()
16:07:05.050 (50780767)|SYSTEM_CONSTRUCTOR_EXIT|[28]|<init>()

However, if we remove the dynamic component from the page, so the page becomes

<apex:page controller="Problem_GenerateDynamicComponent">
    <c:Problem_Demonstration_1 />
</apex:page>

Then suddenly the constructor the Component is run before the constructor of the Page. Has anybody seen this before? And why is that?

34.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
16:06:22.040 (40634685)|EXECUTION_STARTED
16:06:22.040 (40667058)|CODE_UNIT_STARTED|[EXTERNAL]|066250000000JiG|VF: /apex/Problem_Demonstration_3
16:06:22.042 (42640889)|CODE_UNIT_STARTED|[EXTERNAL]|01p250000009Ntp|Problem_Demonstration_1 <init>
16:06:22.042 (42658248)|SYSTEM_MODE_ENTER|true
16:06:22.043 (43205424)|METHOD_ENTRY|[1]|01p250000009Ntp|Problem_Demonstration_1.Problem_Demonstration_1()
16:06:22.043 (43222573)|METHOD_EXIT|[1]|Problem_Demonstration_1
16:06:22.043 (43468175)|SYSTEM_METHOD_ENTRY|[22]|System.debug(ANY)
16:06:22.043 (43500069)|USER_DEBUG|[22]|DEBUG|Constructor of Component : Problem_Demonstration_1
16:06:22.043 (43508225)|SYSTEM_METHOD_EXIT|[22]|System.debug(ANY)
16:06:22.043 (43691101)|SYSTEM_METHOD_ENTRY|[24]|Problem_Demonstration_1.__sfdc_someAccount(Account)
16:06:22.043 (43743567)|SYSTEM_METHOD_EXIT|[24]|Problem_Demonstration_1.__sfdc_someAccount(Account)
16:06:22.043 (43825144)|SYSTEM_CONSTRUCTOR_ENTRY|[28]|<init>()
16:06:22.043 (43855761)|SYSTEM_CONSTRUCTOR_EXIT|[28]|<init>()
16:06:22.043 (43877669)|SYSTEM_METHOD_ENTRY|[28]|Problem_Demonstration_1.__sfdc_possibleProperties(List<System.SelectOption>)
16:06:22.043 (43929082)|SYSTEM_METHOD_EXIT|[28]|Problem_Demonstration_1.__sfdc_possibleProperties(List<System.SelectOption>)
16:06:22.043 (43943397)|SYSTEM_METHOD_ENTRY|[29]|Problem_Demonstration_1.__sfdc_possibleProperties()
16:06:22.043 (43995881)|SYSTEM_METHOD_EXIT|[29]|Problem_Demonstration_1.__sfdc_possibleProperties()
16:06:22.044 (44022911)|SYSTEM_METHOD_ENTRY|[29]|List<System.SelectOption>.add(Object)
16:06:22.044 (44042496)|SYSTEM_METHOD_EXIT|[29]|List<System.SelectOption>.add(Object)
16:06:22.044 (44053022)|SYSTEM_METHOD_ENTRY|[30]|Problem_Demonstration_1.__sfdc_possibleProperties()
16:06:22.044 (44072498)|SYSTEM_METHOD_EXIT|[30]|Problem_Demonstration_1.__sfdc_possibleProperties()
16:06:22.044 (44086776)|SYSTEM_METHOD_ENTRY|[30]|List<System.SelectOption>.add(Object)
16:06:22.044 (44100145)|SYSTEM_METHOD_EXIT|[30]|List<System.SelectOption>.add(Object)
16:06:22.044 (44115711)|CODE_UNIT_FINISHED|Problem_Demonstration_1 <init>
16:06:22.044 (44375180)|CODE_UNIT_STARTED|[EXTERNAL]|01p250000009Ntp|Problem_Demonstration_1 get(someAccount)
16:06:22.044 (44384546)|SYSTEM_MODE_ENTER|true
16:06:22.044 (44398607)|CODE_UNIT_STARTED|[EXTERNAL]|01p250000009Ntp|someAccount
16:06:22.044 (44410368)|CODE_UNIT_FINISHED|someAccount
16:06:22.044 (44424634)|CODE_UNIT_FINISHED|Problem_Demonstration_1 get(someAccount)
16:06:22.045 (45922546)|CODE_UNIT_STARTED|[EXTERNAL]|01p250000009Nus|Problem_GenerateDynamicComponent <init>
16:06:22.045 (45934668)|SYSTEM_MODE_ENTER|true
16:06:22.045 (45997111)|METHOD_ENTRY|[1]|01p250000009Nus|Problem_GenerateDynamicComponent.Problem_GenerateDynamicComponent()
16:06:22.046 (46010530)|METHOD_EXIT|[1]|Problem_GenerateDynamicComponent
16:06:22.046 (46096131)|SYSTEM_METHOD_ENTRY|[6]|System.debug(ANY)
16:06:22.046 (46113706)|USER_DEBUG|[6]|DEBUG|Constructor of Page : Problem_GenerateDynamicComponent
16:06:22.046 (46120600)|SYSTEM_METHOD_EXIT|[6]|System.debug(ANY)
16:06:22.048 (48441182)|SYSTEM_METHOD_ENTRY|[7]|Problem_GenerateDynamicComponent.__sfdc_component(ApexPages.Component)
16:06:22.048 (48495773)|SYSTEM_METHOD_EXIT|[7]|Problem_GenerateDynamicComponent.__sfdc_component(ApexPages.Component)

1 Answer 1

1

I recommend you review Order of Execution for Visualforce Page Get Requests where you'll find the image below and then read Examples of Visualforce Page Execution Order.

Order of Execution for Visualforce Page Get Requests

From reading the latter, I think you'll conclude that which constructor is called first is largely a matter of whether the custom component needs something from the page controller before it can be evaluated. If it does, then the Page Controller's constructor out of necessity must be called first so values can be passed into the Component.

If a Dynamic Component doesn't need anything from the page Controller and doesn't have a Static DOM, then it's Constructor can be called first (and should be). It's only after the dynamic Component has finished it's work that the Page controller can finish creating the DOM to send to the browser and calculate the view state needed to render the page. With a static Component, the DOM for the Component will be known in advance, allowing the Page Controller to send the DOM to the browser regardless of whether the static controller has finished it's calculations needed to render it's content or not.

4
  • Yes thanks, I know that chart. I assumed that if it says "1. Evaluate constructors on controller", that means that the Page constructor is called at that point. But apparenty it is not always. So what does "evaluate" then really mean? Commented Sep 9, 2015 at 21:17
  • If you study the examples and put everything in context of that section of the reference material, you'll quickly conclude that "evaluate" refers to the getters and what's returned in the setters. It evaluates the "inputs" and "outputs" before calling any of the constructors.
    – crmprogdev
    Commented Sep 10, 2015 at 20:13
  • Well, I've read through both pages again, but they seem to contradict with what you're saying. Whichever example we look at, every example is literally introduced with "1. The first thing that happens in a get request is that constructor methods on the custom controller and controller extension are called". Then later: "2. The next step in a get request is to create the custom components and execute constructor methods on associated controllers or controller extensions. " Consequently, the component should never be constructed first. Why would it? Commented Sep 11, 2015 at 13:42
  • You misunderstood my comments. It "evaluates" what happens when get requests are made to each of the controllers before deciding which one to call first. In the case of a dynamic controller, the controller may not require the output from the primary controller in order to be instantiated OR the primary controller may need something from the dynamic component controller before the page controller can be called if the page is to render properly and the DOM constructed to send to the browser. Not all situations are covered in the examples.
    – crmprogdev
    Commented Sep 11, 2015 at 13:59

You must log in to answer this question.

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