11

I was browsing through Visual Force Dev cookbook and was trying to understand the sequence of execution of the controller methods ( constructor, getter, action method) while invoking the action method via the actionpoller.

Basically would like to know the below: [1] When is the Constructor called; is it when the VF connected with the controlled is rendered everytime? [2] When is Getter and Action methods called? What should be the sequence in this example?

I tried to get some debug dumps which suggests the sequence as ** Constructor -> Getter -> Action Method ** which is confusing as Action Method is call first as you could see in the snippet below.

public with sharing class PollerController{
    public List<String> states=new List<String>{'Connecting to Bank', 'Authorised', 'Complete'};
    private Integer stateIdx=0;
    public String getPaymentState()
    { return states[stateIdx];}
    public PageReference movePayment() 
    {
        stateIdx++;
        return null;
    }
}

public with sharing class PollerController 
{
    Public PollerController(){
        system.debug('** Inside Constructor **'); }
    public List<String> states=new List<String>{
      'Connecting to Bank', 'Authorising',  'Authorised', 'Complete'};
    private Integer stateIdx=0;
    public String getPaymentState()
    {
       system.debug('** Inside getPaymentState Method **');
       return states[stateIdx];
    }
    public PageReference movePayment() 
    {
        system.debug('** Inside Action Method **');
        stateIdx++;
        return null;
    }
}

3 Answers 3

8

You can find full details of the order of execution for Visualforce pages at:

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#CSHID=pages_controller_lifecycle.htm|StartTopic=Content%2Fpages_controller_lifecycle.htm|SkinName=webhelp

Some things are unexpected, in that getters can execute more than once when the page is being rendered - presumably this is because it can take multiple passes with a single page to generate the output.

Be wary of trying to infer the ordering, as the underlying code may change without warning and alter the order. Only trust the docs!

7

The constructor is called when a page using the controller is first viewed (unless you've just come from another page that uses the same controller).

Getters are called as the page is rendered to fill in information, then actions are typically fired after in response to a button press or similar. <apex:actionPoller> renderes every n seconds where you define the time, and if it re-renders part of the page (or the whole page) the getters would be called after the action is performed.

I think the only action performed before getters is the one specified in the <apex:page> tag itself, but don't quote me on that.

<apex:page init="{!myAction}">
2
  • 2
    Here's the SFDC documentation on the order for GET and POST: salesforce.com/us/developer/docs/pages/Content/…
    – Mark Pond
    Commented Nov 26, 2013 at 7:19
  • 3
    The action in the apex:page tag makes no guarantees about when it will execute - this is why the VF docs tell you not to use it for initialisation, as the getters may run before you have initialised the objects they are using. Commented Nov 26, 2013 at 13:13
0

Recently, I had set a variable value from javascript and was expecting the controller setter to set the values in the constructor, but realised it does not work that way. This made me explore the order sequence of getter, setter,constructor and action method in detail. Here are my learnings :
Page Load:
Constructor -- > init method -->Getter
Event(click) invoking an action method:
Getter--> Setter-->ActionMethod --> Getter

1

You must log in to answer this question.

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