Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 4 down vote accepted

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

share|improve this answer
    
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. – Chris Payne Mar 27 at 14:31
1  
You could always have them call a different entry point. – Eric Mar 27 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? – Chris Payne Mar 27 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 Mar 27 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. – Chris Payne Mar 27 at 14:37

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;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.