9

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.

2
  • I think this is because the actionFunction expects to get 1 parameter. Commented Mar 5, 2014 at 7:44
  • I have tried this as well like setState('update') but result is same. Commented Mar 5, 2014 at 7:47

2 Answers 2

10

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;"/>
6
  • 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? Commented Mar 5, 2014 at 9:24
  • 1
    Now method is being called but parameter is not being passed. i am getting null in controller – Commented Mar 5, 2014 at 9:41
  • Value is being passed on controller if i put rerender="xyx" in actionFuction. Do you know why? Commented Mar 5, 2014 at 9:45
  • 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: help.salesforce.com/apex/… - it may be that its not working for action functions now. Commented Mar 5, 2014 at 10:06
  • 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? Commented Mar 5, 2014 at 10:08
4

Try this instead:

When you define parameter to actionfunction then action javascript accept it as argument.

<apex:page controller="TestActionFunctionWithoutAction">
  <apex:form>

     <apex:commandButton title="Apply and Continue" value="Apply and
         Continue" onclick="setState()" rerender="xyx"/>      
     <apex:actionFunction id="setState" action="{!click}" name="setState" >
      <apex:param name="state" assignTo="{!state}" value="update"/>
     </apex:actionFunction>

  </apex:form>
</apex:page>

Apex:

public class TestActionFunctionWithoutAction
{
   public pagereference click()
   {
     System.assert(false,'That it!');
     System.debug('------------------------------------- ');
     return ApexPages.currentPage();
   }

   public void abc()
   {
   }
}

Result:

Assertion Failed: That it! Error is in expression '{!click}' in page ashcloud:testactionfunction An unexpected error has occurred. Your development organization has been notified.

15
  • I have tried this as well but result is same. Commented Mar 5, 2014 at 7:46
  • @PramodKumar did you add autocomplete attribute to check?
    – Ashwani
    Commented Mar 5, 2014 at 7:48
  • no didn't add that. Let me try with that Commented Mar 5, 2014 at 7:49
  • @PramodKumar use actionfunction code I posted in answer.
    – Ashwani
    Commented Mar 5, 2014 at 7:50
  • getting "test" in alert but server method is not being called Commented Mar 5, 2014 at 7:54

You must log in to answer this question.

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