Select your Language

Difference between Action function and Remote action ,Action support and action poller

Remote action
1. If we want to call apex class method from java Script then we need to define apex class as                     @remoteAction annotation
2. And that class should be Global and method should be static
3. We use this because  normally  we can not pass the parameter in apex class by java script function but with the help of remote action we can pass the parameter in apex class .
4. It is a asynchronous method and it does't have any data in view state
5. Remote Action methods require less bandwidth, and server processing time, because only the data you submit is visible and the view state is not transferred .
6. Remote Action methods have to update the DOM manually using explicit JavaScript.
7. Remote Action methods can return data directly back to the calling JavaScript, but cannot update the page’s view state.
8. It  retrieve the result as a java script object for manipulation,

Action function
1. it is used in vf page, it is a visual force tag, it is allow you to invoke controller action from the vf page asynchronously via Ajax call
2. it is used whenever we have to call a method from apex class but in it we can not transfer the parameter in action function
3. Action Function doesn’t let you get retrieve data
4. Action Function methods are instance methods, and so can see the entire page in view state.
5. Action Function methods automatically update the Visualforce DOM
6. It  can provide a standard interface for showing a loading status through apex:actionStatus.

Action Support
1. It used when we want to perform an action on a particular event of any control like onchange of any text box or picklist.
2. It call the controller action without java script but action function call the controller method with the help of java script.
3. This component adds Ajax request to any other Visualforce component.

Action Poller
1. A timer that sends an AJAX request to the server according to a time interval that you specify.
2. Each request can result in a full or partial page update.
3. This is timer component which can send AJAX request on pre-defined interval.
4. Minimum interval is 5 sec and default is 60 sec.
<apex:actionPoller action=”{!incrementCounter}” rerender=”counter” status=”counterStatus” interval=”50″ />



NOTE:    apex:actionFunction is easier to use and requires less code, while @RemoteAction offers more flexibility.
               @RemoteAction helps in reducing View State size and also provides you near real time transaction. Both are called for server side methods

Example:Action Support

VF PAGE

<apex:page controller="ActionCon">
    <apex:form >
        <apex:pageBlock title="Example">
           <apex:actionRegion >
                 Enter Name : <apex:inputText value="{!name}"  />
                Enter City : <apex:inputText value="{!city}">
                <apex:actionSupport event="onchange" action="{!call}" reRender="one" />
                </apex:inputText><br/><br/>
            </apex:actionRegion>
            <apex:outputLabel value="{!result}" id="one" />
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:

public class ActionCon {
    public String name     {set;get;}
    public String city     {set;get;}
    public String result   {set;get;}
    public void call(){
        result='Name: '+name+'  :City : '+city;
    }

}
==================================================================

Action Function:

<apex:page controller="ActionFunction">
    <apex:form >
        <script>
        function javaScriptMethod(){
            alert('Calling Action Function');
            myactionfun();
        }
        </script>
        <apex:actionFunction name="myactionfun"  action="{!actionFunctionTest}" reRender="pgBlock" />
        <apex:pageBlock title="Test Action Function Test">
            <apex:pageblockSection >
                <apex:inputText value="{!dummyString}" ></apex:inputText>
                <apex:inputcheckbox onclick="javaScriptMethod();" />
            </apex:pageblockSection>
        </apex:pageBlock>
        <apex:pageblock title="Output of Action Funtion" id="pgBlock">
            <apex:pageblockSection id="pbSection" >
                {!dummyString}
            </apex:pageblockSection>
        </apex:pageblock>
    </apex:form>

</apex:page>

Controller

public class ActionFunction{
    public static String dummyString { get; set; }
    public void actionFunctionTest()
    {
        dummyString = 'Value from Action Function';
    }
   
   

}
========================================================================
Remote action

Apex class:

1

global with sharing class ContactJs {  
    cense   https://store.webkul.com/license.html
    */
    public ContactJs() { }    

    @RemoteAction //the function to be called in remote action should use this annotation
    global static list<Contact> getcon() {
        //function should be static and global else it will throw error
        list<Contact> con1 = [SELECT id,name FROM contact limit 5];
        if(con1!=null && !con1.isEmpty()){        
            return con1;        
        }else{        
            return  new list<contact>();        
        }
    }
}

2 VF Page

<apex:page controller="ContactJs">
    
    <script type = "text/javascript">
    function getRemoteContact() {
        var a;
        Visualforce.remoting.Manager.invokeAction(
            //Invoking controller action getcon
            '{!$RemoteAction.ContactJs.getcon}',
            
            function(result, event){
               if(event.status){
                    document.getElementById('remoteContactId').innerHTML = 'Contact Name: <br/><br/>';
                    for(a=0;a<result.length;a++){                        
                        document.getElementById('remoteContactId').innerHTML +=  result[a].Name +'<br/>';                    
                    }                                       
               }               
            },
            {escape: true}
        );
    }
    </script>

    <button onclick="getRemoteContact()">Get Contact</button>
    <div id="responseErrors"></div>
    <apex:pageBlock id="block">        
        <apex:pageBlockSection id="blockSection" columns="2">
                <span id="remoteContactId"></span>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>



No comments:

Post a Comment