+ Start a Discussion
sreejasreeja 

how to use the remote action in salesforce, any small examples working with custom objects

how to when to use the remoteaction in salesforce, any small examples to start ... i??

 how do the remote action will work in the salesforce ..
thanks 
sree
Best Answer chosen by sreeja
Parmanand PathakParmanand Pathak
Hi Vani,

Try below code to display list of contacts using RemoteAction - 
 
global with sharing class ContactJs {  
   
    public ContactJs() { } // empty constructor    
 
    @RemoteAction 
    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 20];
        if(con1!=null && !con1.isEmpty()){        
            return con1;        
        }else{        
            return  new list<contact>();        
        }
    }
}
 
<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){
               //We can access the records through the parameter result
               //event.status determines if there is error or not 
               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: false, timeout:3000, buffer: false}
        );
    }
        
    </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>

In order to help the community, mark it as best answer if it helps you.

Thanks,
Parmanand Pathak

All Answers

Parmanand PathakParmanand Pathak
Hi Vani,

Try below code to display list of contacts using RemoteAction - 
 
global with sharing class ContactJs {  
   
    public ContactJs() { } // empty constructor    
 
    @RemoteAction 
    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 20];
        if(con1!=null && !con1.isEmpty()){        
            return con1;        
        }else{        
            return  new list<contact>();        
        }
    }
}
 
<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){
               //We can access the records through the parameter result
               //event.status determines if there is error or not 
               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: false, timeout:3000, buffer: false}
        );
    }
        
    </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>

In order to help the community, mark it as best answer if it helps you.

Thanks,
Parmanand Pathak
This was selected as the best answer
Madhukar_HeptarcMadhukar_Heptarc
HI Sree,
Remote action function in salesforce allows user to access any method from any class through javasrcipt methods, and get the result as a javascript object for further manipulation.
It will works like whenever your are clicking the command button it will call the Javascriprt funtion. on that javascript funtion it will calls the funtion
Please refer the below Visualforce page :$RemoteAction.ContactJs.getcon
Points to remember while implementing remote action function:
Remote action method should have @RemoteAction annotation.

It will goto controller and it will check the RemoteAction method and it will check for con() method.
On that method whatever we are retriving in the SOQL Query display returns and It will displays in the Screen by clicking the Commmand button.
Apex Class :
==========
global with sharing class ContactJs {  
    public ContactJs() { } // empty constructor    
    @RemoteAction //the function to be called in remote action should use this annotation
    public static list<Account> getcon() {
        //function should be static and global else it will throw error
        list<Account> con1 = [SELECT id,name FROM Account limit 5];
        if(con1!=null && !con1.isEmpty()){        
            return con1;        
        }else{        
            return  new list<Account>();        
        }
    }
}

Visual force 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){
               //We can access the records through the parameter result
               //event.status determines if there is error or not 
               if(event.status){
                    document.getElementById('remoteContactId').innerHTML = 'Account 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>
Please refer the salesforce Document Link :
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting_summary.htm
https://webkul.com/blog/remote-action-function-visualforce-page/

Try the above code to understand the Remote action.
Please let me know any help required. If it helps you please make it as solved.

Regards,
Madhukar.