Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
    
I think this is because the actionFunction expects to get 1 parameter. –  Lex Mar 5 at 7:44
    
I have tried this as well like setState('update') but result is same. –  Pramod Kumar Mar 5 at 7:47

2 Answers 2

up vote 3 down vote accepted

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;"/>
share|improve this answer
    
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? –  Pramod Kumar Mar 5 at 9:24
    
Now method is being called but parameter is not being passed. i am getting null in controller – –  Pramod Kumar Mar 5 at 9:41
    
Value is being passed on controller if i put rerender="xyx" in actionFuction. Do you know why? –  Pramod Kumar Mar 5 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. –  Bob Buzzard Mar 5 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? –  Pramod Kumar Mar 5 at 10:08

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.

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

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.