3

I have a Visualforce page that is backed by a controller. The page will make XHR requests back to the controller upon a number of events by making use of apex:actionSupport eg <apex:actionSupport event="onchange" action="{!UpdateWhatId}" reRender="inf_whatId,inf_location"/>.

I need the ability to distinguish between these XHR requests, and the initial page renders from within my controller.

How can I do this?

2 Answers 2

4

I guess you could add this to the part of your page where the method is called again:

<apex:actionSupport event="onchange" action="{!UpdateWhatId}" reRender="inf_whatId,if_location">
   <apex:param name="myFlag" value="true" assignTo="{!myBooleanValue}"/>
</apex:actionSupport>

Then in your class:

public boolean myBooleanValue {get;set;}

public void UpdateWhatId(){
    If(myBooleanValue){
       //do stuff when called from within page
    }else{
       //do stuff from action of vf pageload
    }

    myBooleanValue = false; //reset
}

Basically using the param tag you can set the value of a property in your class and tell if it came from where you wanted it or if the method was executed as part of the page load.

You could also have them call a different entry point:

add to class

public void fromPage(){
   myBooleanValue = true;
   updatewhatid();
}

Or on load you set the value of myBoolean to true when the method is executed and then you know it was ran. Subsequent calls to the method knows that it should enter the fromPage part instead of the on load part

public void UpdateWhatId(){
    If(myBooleanValue){
       //do stuff from action of vf pageload
    }else{
       //do stuff when called from within page
    }

    myBooleanValue = true; //Set to true to indicate it was ran the first time
}

It all depends on where this question goes after you have implemented the logic check

6
  • I was afraid that was going to be the answer. Unfortunately I have a number of actionSupport tags on this page and I was looking for a way to do this without duplication of the param tags. Commented Mar 27, 2016 at 14:31
  • 1
    You could always have them call a different entry point.
    – Eric
    Commented Mar 27, 2016 at 14:31
  • Out of curiosity, why would I need the line to reset the bool? Does an instance of the controller not get created for every request and therefore this value would effectively get reset on the next request? Commented Mar 27, 2016 at 14:32
  • @ChrisPayne - No. The properties in your controller remain throughout the lifecycle of the VF page as long as they are not static or transient
    – Eric
    Commented Mar 27, 2016 at 14:33
  • When you say "lifecycle of the VF page", do you mean the lifecycle of the HTTP request? Or, do you mean the initial HTTP request that renders the page and then any subsequent XHR requests made to the controller? I understood it was the former. Commented Mar 27, 2016 at 14:37
2

The environment doesn't tell you directly. However, you could build a private function and up to two public functions (depending on how the initial calling happens). Like so:

void doAction(Boolean first) {
    if(first) {...

public void doAction() {
    doAction(false);
}

Alternatively, depending on your use case, you could also just set a variable after the first time the function is called:

Boolean first = true;
public void doAction() {
    if(first) {
        ...
    }
    first = false;
}

You must log in to answer this question.

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