function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Koustubh KulkarniKoustubh Kulkarni 

Can anyone explain me how apex:actionfunction works? with simple example?

Best Answer chosen by Koustubh Kulkarni
KaranrajKaranraj
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/

All Answers

Amit Chaudhary 8Amit Chaudhary 8
apex:actionFunction

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.

Example 1:-
<!-- Page: -->
<apex:page controller="exampleCon">
    <apex:form>
        <!-- Define the JavaScript function sayHello-->
        <apex:actionFunction name="sayHello" action="{!sayHello}" rerender="out" status="myStatus"/>
    </apex:form>

    <apex:outputPanel id="out">
    <apex:outputText value="Hello "/>
    <apex:actionStatus startText="requesting..." id="myStatus">
        <apex:facet name="stop">{!username}</apex:facet>
    </apex:actionStatus>
    </apex:outputPanel>
            
    <!-- Call the sayHello JavaScript function using a script element-->
    <script>window.setTimeout(sayHello,2000)</script>
            
    <p><apex:outputText value="Clicked? {!state}" id="showstate" /></p> 
            
    <!-- 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>

/*** 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';
}

Exmaple 2:-
http://sfdcsrini.blogspot.com/2014/07/visualforce-action-function-example.html

Eample 3:-
http://www.cloudforce4u.com/2013/06/actionfunction-in-apex.html

Please let us know if this will help u

Thanks
AMit Chaudhary
KaranrajKaranraj
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/
This was selected as the best answer
SFDC GuestSFDC Guest
Here is the example for actionFunction.
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000kCIYIA2

Thank You,
Sohel Mohd
Newbie999Newbie999
Hi,

The simplest way to understand <apex:actionFunction> is by watching the below video:
https://www.youtube.com/watch?v=VHIPL6kmWoc

Hope it helps :)