+ Start a Discussion
Lukesh KarmoreLukesh Karmore 

when to use Apex:ActionFunction

Hello forum,
I want to know when to use Apex:ActionFunction .
we call  the controller method from javascript function using ActionFunction . I read this line in many blogs but why i call  controller method from javascript function, when actually i need it .
can any one explain me thank you in advance.
 
sakhisakhi
A component that provides support for invoking controller action methods directly from JavaScript code using an AJAX request. An <apex:actionFunction> component must be a child of an <apex:form> component.
<apex:page controller="exampleCon">
<!-- Add the onclick event listener to a panel. When clicked, the panel triggers
    the methodOneInJavascript actionFunction with a param -->
    <apex:outputPanel onclick="methodOneInJavascript('Yes!')" styleClass="btn"> 
        Click Me 
    </apex:outputPanel>
    <apex:form>
    <apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="showstate">
        <apex:param name="firstParam" assignTo="{!state}" value="" />
    </apex:actionFunction>
    </apex:form>
</apex:page>
In the above visualforce page javascript method in the onclickevent will call the apex controller method with the help of actionfunction.
Apex controller. With the help of actionfunction we can able to make a call to the apex controller class
/*** Controller ***/
public class exampleCon {
    String uname;

    public String getUsername() {
        return uname;
    }
            
    public PageReference sayHello() {
        uname = UserInfo.getName();
        return null;
    }
            
    public void setState(String n) {
        state = n;
    }
            
    public String getState() {
        return state;
    }
            
    public PageReference methodOne() {
        return null;
    }
            
    private String state = 'no';
}
Check other blog post link which might be helpful for you

http://sfdcsrini.blogspot.com/2014/07/visualforce-action-function-example.html
http://www.salesforcetutorial.com/actionfunction-tag/

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Lukesh,

>> https://developer.salesforce.com/forums/?id=906F0000000BTIPIA4

The above link has an example regarding how to use apex:actionfunction and an implementation.

A component that provides support for invoking controller action methods directly from JavaScript code using an AJAX request. An <apex:actionFunction> component must be a child of an <apex:form> component.
 
<apex:page controller="exampleCon">
<!-- Add the onclick event listener to a panel. When clicked, the panel triggers
    the methodOneInJavascript actionFunction with a param -->
    <apex:outputPanel onclick="methodOneInJavascript('Yes!')" styleClass="btn"> 
        Click Me 
    </apex:outputPanel>
    <apex:form>
    <apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="showstate">
        <apex:param name="firstParam" assignTo="{!state}" value="" />
    </apex:actionFunction>
    </apex:form>
</apex:page>

In the above visualforce page javascript method in the onclickevent will call the apex controller method with the help of actionfunction.
Apex controller. With the help of actionfunction we can able to make a call to the apex controller class
 
/*** Controller ***/
public class exampleCon {
    String uname;

    public String getUsername() {
        return uname;
    }
            
    public PageReference sayHello() {
        uname = UserInfo.getName();
        return null;
    }
            
    public void setState(String n) {
        state = n;
    }
            
    public String getState() {
        return state;
    }
            
    public PageReference methodOne() {
        return null;
    }
            
    private String state = 'no';
}

Check other blog post link which might be helpful for you

http://sfdcsrini.blogspot.com/2014/07/visualforce-action-function-example.html
http://www.salesforcetutorial.com/actionfunction-tag/

Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.  

Thanks.
Lukesh KarmoreLukesh Karmore
Thank you both of you , but i want to know why we do this  ,what is the need of calling controller method from JavaScript