2
<apex:page controller = "controllerClass" extensions ="ExtensionClass" action="{!go}">
    <c:TestComponent/>
<apex:page>

<apex:component  controller = "componentController">
</apex:component >

What should be execution order on executing the VF

8
  • What's your question?
    – superfell
    Nov 5, 2015 at 18:13
  • I'm playing with this, but I don't see any serious implications of why the constructor order makes any difference...?
    – sfdcfox
    Nov 5, 2015 at 18:32
  • Why would action attribute make that difference @ sfdcfox
    – Sara
    Nov 5, 2015 at 18:41
  • @superfell, the order of execution as I showed in log thing is not same is both just because of action attribute.
    – Sara
    Nov 5, 2015 at 18:42
  • That's not a question.
    – superfell
    Nov 5, 2015 at 18:43

1 Answer 1

6

Technically, this problem affects certain types of inter-controller communications. I believe I've run into this bug before, but support said that there was no impending fix for the problem. You shouldn't try to attempt controller communications before all of them are constructed, simply because the order of constructors is not necessarily dependable, except as defined by the programming API.

For example, you should not assume that extension controllers are constructed in any particular order, but you can assume that the page's controller will be constructed before the extension controllers are (because we're given a reference to the object by the programming interface).

For example, this code would be affected by the bug and is unreliable:

public class ComponentController {
    PageController c;
    public ComponentController() {
        c = PageController.self;
    }
}

public class PageController {
    public static PageController self;
    public PageController() {
        self = this;
    }
}

Since we can't determine the order of operations, the component controller may end up with a null reference. Instead, if you want to properly enable communications between the controller and the page, you must use apex:attribute.

public class ComponentController {
    PageController c;
    public void setPageController(PageController controller) {
        if(c == null) {
            c = controller;
            // Do stuff here
        }
    }
}

public class PageController {
    public PageController getSelf() {
        return this;
    }
}

You'd assign it through the usual means:

<apex:component controller="ComponentController">
    <apex:attribute name="pageControllerAttribute" assignTo="{!pageController}" description="" type="PageController" />
    ...
</apex:component>

And on the page:

<apex:page controller="PageController">
    <c:customComponent pageControllerAttribute="{!self}" />
</apex:page>

This is a good idea anyways, because statics are not retained across page transactions (they are not in the view state), so you still need to copy a reference to the controller. Using an apex:attribute, you can guarantee all constructors have been called.

You may want to contact support, but I'm pretty sure they're going to tell you that depending on the order of operations of constructors, getters, and/or setters is not supported.

You must log in to answer this question.

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