Activity Forums Salesforce® Discussions What is remote action in Salesforce?

  • Achintya

    Member
    August 30, 2019 at 7:49 am

    Remote action function in salesforce allows the user to access any method from any class through javascript methods, and get the result as a javascript object for further manipulation.

    While implementing remote action function remember:

    Remote action method should have @RemoteAction annotation. The method should also be Global and Static
    Implementation
    Let’s start with the controller code:

    global with sharing class ContactJs {  
    
        public ContactJs() { }  
        @RemoteAction
        global static list<Contact> getcon() {
            list<Contact> con1 = [SELECT id,name FROM contact limit 5];
            if(con1!=null && !con1.isEmpty()){        
                return con1;        
            }else{        
                return  new list<contact>();        
            }
        }
    }

    Now the Visualforce Page:

    <apex:page controller="ContactJs">
        <script type = "text/javascript">
        function getRemoteContact() {
            var a;
            Visualforce.remoting.Manager.invokeAction(
                '{!$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>

     

  • Yogesh

    Member
    August 30, 2019 at 8:06 am

    JavaScript remoting in Visualforce provides support for some methods in Apex controllers to be called via JavaScript.

    JavaScript remoting has three parts:
    The remote method invocation you add to the Visualforce page, written in JavaScript.
    The remote method definition in your Apex controller class. This method definition is written in Apex, but there are few differences from normal action methods.
    The response handler callback function you add to or include in your Visualforce page, written in JavaScript.
    To use JavaScript remoting in a Visualforce page, add the request as a JavaScript invocation.

    for example cheak it oput this link:-https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting_example.htm

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos