+ Start a Discussion
TarentTarent 

Difference between action function and action support function

"for interview purpose" give me best Difference between action function and action support function??/

Navatar_DbSupNavatar_DbSup

Hi,

 

ActionFunction : provides support for invoking controller action methods directly from JavaScript code using an AJAXrequest

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.

 

eg.

actionFunction : provides support for invoking controller action methods directly from JavaScript code using an AJAXrequest

 

Example :

<!-- 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 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.

eg.

<!-- 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;

}

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

sandeep@Salesforcesandeep@Salesforce

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.

Shahroz BeigShahroz Beig

Here are the high-level differences:

  1. ActionFunction allows us to call the server side Apex method from the JS in the VisualForce Page. whereas ActionSupport 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. For eg. Apex:inputText does not support onChange event so we can use actionSupport to add onChange support and call the server side
    <apex:inputText value="{!accName}"> <apex:actionSupport event="onkeyup" action="{!doQuery}" rerender="pgblock" /> </apex:inputText>
  2.  An <apex:actionFunction> component must be a child of an <apex:form> component but  <apex:actionSupport> can be used anywhere.