Calling apex:actionFunction on apex:commandButton's onclick event

  • Can we call apex:actionFunction on apex:commandButton's onclick event?

    Actually i am trying to do this but controller's method is not being called.

    i am doing something like below -

     <apex:commandButton title="Apply and Continue" value="Apply and
     Continue" onclick="setState()"/>
    

    Action function is declared like below -

    <apex:actionFunction id="setState" action="{!checkClick}" name="setState" >
          <apex:param name="state" assignTo="{!state}" value="update"/>
    </apex:actionFunction>
    

    Controller Code -

      public PageReference checkClick() {
          system.assert(false, 'In -' + state);
          return null;  
      }
    

    controller's checkClick is not being called. Page just get refreshed.

    I think this is because the actionFunction expects to get 1 parameter.

    I have tried this as well like setState('update') but result is same.

  • Bob Buzzard

    Bob Buzzard Correct answer

    7 years ago

    You have a race condition here - once the onclick handler completes the command button will continue with its normal flow and submit the form.

    Thus both the commandbutton and the actionfunction components will both try to submit the form, and as the commmandbutton goes first I'd expect it to win, although its not an exact science.

    If you want to use an actionfunction in this way, you have to stop the normal behavior of the commandbutton. One way to do this is to return false from the onclick handler:

    <apex:commandButton title="Apply and Continue" value="Apply and Continue" 
                        onclick="setState(); return false;"/>
    

    ok got your point but why does code starts working if i put rerender="xyx" in commandbutton? Secondly onclick handler will complete when your actionFuction action is called i believe and after than commandbutton action should called. what do u think?

    Now method is being called but parameter is not being passed. i am getting null in controller –

    Value is being passed on controller if i put rerender="xyx" in actionFuction. Do you know why?

    There is an issue that has been around for years that if you don't have a rerender attribute the parameter doesn't go to the controller, but I thought that was just for command buttons: http://help.salesforce.com/apex/HTViewSolution?id=000002664&language=en_US - it may be that its not working for action functions now.

    Thanks a lot . Secondly onclick handler will complete when your actionFuction action is called i believe and after than commandbutton action should called. what do u think?

    When the actionfunction is called a postback takes place. If you then allow the command button to continue, one of the postbacks will win and the other will be discarded. You should put all of the functionality in one place and just use that postback I would say, otherwise the results will be unpredictable.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM