Click Me /*** 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. Example :    /*** Controller: ***/ public class exampleCon { Integer count = 0; public PageReference incrementCounter() { count++; return null; } public Integer getCount() { return count; } } @RemoteAction  is an Apex annotation used to mark a method as being available for javascript remoting. This allows you to call the method from javascript yourself and retrieve the result as a javascript object for manipulation, as per the example in the documentation. They are static and global, and hence don't have access to your current controller variables and methods. For example, please refer below link. https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting_example.htm   https://developer.salesforce.com/forums/?id=906F0000000ADFwIAO   Hope this helps you! Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.   Thanks and Regards Sandhya  ", "upvoteCount": 3, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T48wASAR", "datePublished": "2016-09-22T03:29:34.000Z", "author": { "@type": "Person", "name": "Sandhya Reddy Kasarla", "url": "https://trailblazers.salesforce.com/profileView?u=0053A00000E3HWGQA3", "affiliation": { "@type": "Organization", "name": "--" } } } ] } } Skip to main content
Hi,

I am having difficulty in understanding  What is the purpose and difference between Action Function, Remote Action and Action Support. Pls explain with a Example or a scenario. 

thanq.
3 个回答
  1. 2016年9月22日 03:29
    Hi Vivek,

    Action Function

    Action function used to call the server side method using JavaScript.

    Explanation:-

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

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

    Used when we need to perform the similar action on various events. Een though you can use it in place of actionSupport as well where the only event is related to only one control.

    Example :

     

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

    Example : 

     

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

    }

    }

    @RemoteAction is an Apex annotation used to mark a method as being available for javascript remoting. This allows you to call the method from javascript yourself and retrieve the result as a javascript object for manipulation, as per the example in the documentation. They are static and global, and hence don't have access to your current controller variables and methods.

    For example, please refer below link.

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

     

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

     

    Hope this helps you!

    Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.

     

    Thanks and Regards

    Sandhya

     
0/9000