apex:actionFunction is not calling Apex method - Work around

Sometimes, apex:actionFunction is not calling Apex method. The Work around for this is, the Javascript function should return true or false. In apex:commandButton, before calling the Javascript function, we should add return. If the Javascript function returns true, then the method will be invoked else it will not invoke the Apex method.

Sample:

Visualforce page:

<apex:page sidebar="false" Controller="Sample" showHeader="true">
<apex:form >
    <apex:commandButton value="show" onClick="return confirmReq();" action="{!show}"/>
</apex:form>
<apex:outputPanel id="jspanel">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script>
        function confirmReq() {
        if(condition) {
            return true;    
        } else {
        {  
            return false;                                
        }
    </script>
</apex:outputPanel>
</apex:page>

Controller:


public class Sample {     
    public sample() {
    }
    public void show() {
    /*...................
    ...................
    ...................
    ...................*/
    }
}


in the above example, show() from Apex will not be executed until the Javascript function returns true.

Cheers!!!

No comments:

Post a Comment