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
srikanthpallasrikanthpalla 

Actionfunction and Actionsupport

please let me know about Actionfunction and actionsupport in VF pages
Best Answer chosen by srikanthpalla
Mahesh DMahesh D
Hi Srikanth,

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. Used when we need to perform similar action on various events. Even though you can use it in place of actionSupport as well where only event is related to only one control.

Ex:
actionFunction : provides support for invoking controller action methods directly from JavaScript code using an AJAXrequest
 
<!-- 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';
}

apex:actionSupport

A component that adds AJAX support to another component, allowing the component to be refreshed asynchronously by the server when a particular event occurs, such as a button click or mouseover.
Used when we want to perform an action on a particular event of any control like onchange of any text box or picklist.
 
<!-- Page: -->
<apex:page controller="exampleCon">
<apex:form>
<apex:outputpanel id="counter">
<apex:outputText value="Click Me!: {!count}"/>
<apex:actionSupport event="onclick"
action="{!incrementCounter}"
rerender="counter" status="counterStatus"/>
</apex:outputpanel>
<apex:actionStatus id="counterStatus"
startText=" (incrementing...)"
stopText=" (done)"/>
</apex:form>
</apex:page>
 
/*** Controller: ***/
public class exampleCon {
Integer count = 0;
public PageReference incrementCounter() {
count++;
return null;
}
public Integer getCount() {
return count;
}
}

--------------------------------------------------------------------------------------------------------------------------------------------------------

1. difference is in case of Action function we invoke AJAX using Java script while in case of Action support we may directly
invoke method from controller
 
2. other difference is Action function may be commonly used from different place on page while action support may only be used for particular single apex component.

All the corresponding examples you can find from below link:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm

https://success.salesforce.com/answers?id=90630000000hU2kAAE

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

http://www.salesforcetutorial.com/actionfunction-tag/

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Srikanth,

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. Used when we need to perform similar action on various events. Even though you can use it in place of actionSupport as well where only event is related to only one control.

Ex:
actionFunction : provides support for invoking controller action methods directly from JavaScript code using an AJAXrequest
 
<!-- 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';
}

apex:actionSupport

A component that adds AJAX support to another component, allowing the component to be refreshed asynchronously by the server when a particular event occurs, such as a button click or mouseover.
Used when we want to perform an action on a particular event of any control like onchange of any text box or picklist.
 
<!-- Page: -->
<apex:page controller="exampleCon">
<apex:form>
<apex:outputpanel id="counter">
<apex:outputText value="Click Me!: {!count}"/>
<apex:actionSupport event="onclick"
action="{!incrementCounter}"
rerender="counter" status="counterStatus"/>
</apex:outputpanel>
<apex:actionStatus id="counterStatus"
startText=" (incrementing...)"
stopText=" (done)"/>
</apex:form>
</apex:page>
 
/*** Controller: ***/
public class exampleCon {
Integer count = 0;
public PageReference incrementCounter() {
count++;
return null;
}
public Integer getCount() {
return count;
}
}

--------------------------------------------------------------------------------------------------------------------------------------------------------

1. difference is in case of Action function we invoke AJAX using Java script while in case of Action support we may directly
invoke method from controller
 
2. other difference is Action function may be commonly used from different place on page while action support may only be used for particular single apex component.

All the corresponding examples you can find from below link:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm

https://success.salesforce.com/answers?id=90630000000hU2kAAE

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

http://www.salesforcetutorial.com/actionfunction-tag/

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Hi Srikanth,

There are to many example are available in Google for same. Please check below post. I hope that will help you
1) https://developer.salesforce.com/forums/?id=906F0000000933OIAQ
2) https://developer.salesforce.com/forums/?id=906F0000000976QIAQ
3) http://www.cloudforce4u.com/2013/06/difference-between-action-support-and.html
4) http://learn4sfdc.blogspot.in/2014/01/explain-actionfunction-actionsupport.html

actionFunction : provides support for invoking controller action methods directly from JavaScript code using an AJAXrequest
 Used when we need to perform similar action on varioud events. Een though you can use it in place of actionSupport as well where only event is related to only one control.
http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_actionFunction.htm
<apex:actionFunction name="sendEmail" action="{!sendEmailFunction}"></apex:actionFunction>
 
<!-- 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';
}




ActionSupport : A component that adds AJAX support to another component, allowing the component to be refreshed asynchronously by theserver when a particular event occurs, such as a button click or mouseover.
Used when we want to perform an action on a perticular eventof any control like onchange of any text box or picklist.
 
<apex:outputpanel id="counter">
<apex:outputText value="Click Me!: {!count}"/>
<apex:actionSupport event="onclick"  action="{!incrementCounter}"  rerender="counter" status="counterStatus"/>
</apex:outputpanel>
User-added image

Let us know if this will help u

Thanks
Amit Chaudhary

 
SFDC GuestSFDC Guest
Hi Sriknath,

Here is the example for actionSupport.
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000kCFoIAM

Thank You,
Sohel Mohd