Skip to main content
sandeep reddy ha preguntado en #Apex
can you please tell me one example remote action using java script please

i did not get the proper example 
3 respuestas
  1. 30 mar 2016, 8:06
    Hi Sandeep,

    This is a way within Salesforce to call methods in a class via javascript. 

    Advantage:

    No forms!

    First of all, you are not required to use forms, Apex or otherwise. This eliminates the need to keep viewstate. By not keeping viewstate you are not passing or storing all of that data for the controller calls. Your calls to the controller have a small footprint and become very efficient. This is especially useful in mobile web development.

    Blazing!

    This leads to the second reason why you would want to use remoting, speed. It is by far the fastest way of calling controller code and passing data to and from the page. You are able to ensure that you are only passing the data you need each time you make a call. Salesforce also bundles multiple subsequent calls by default with no work by you which also improves speed.

    Asynchronous

    The third reason why you might want to use remoting is because it is asynchronous. Since it is asynchronous, you can load the initial page and data for the page and after that is all loaded you can start to lazy load data for the page that might not be prevalent for the inital load. You can even use this method to start loading data for pages the user hasn’t even hit yet. An example of this would be to load the first page and if there is a second page that you know if visited often you can load that in the background so if the user does switch to that second page all the data will be available which leads to an awesome user experience.

    Syntax(block to be in visualforce page):

    [namespace.]controller.method(

    [parameters…,]

    callbackFunction,

    [configuration]

    );

    example:

    MyController.methodOnMyController(

    firstParameter,

    secondParameter,

    function(results, event) {

    if(event.type === 'exception'){

    console.log("exception");

    console.log(event);

    }else if(event.status){

    .each(results, function(i, result){

    doSomethingWithTheResult(result);

    });

    }else{

    console.log(event.message);

    }

    });

    This code is assuming I have a class called MyController and MyController has a method on it called methodOnMyController that takes two parameters. If you have done ajax calls in jQuery this will look very familiar. After the parameters, you have your callback function. The callback function takes the results of the method call as well as the event object which has information on how the remoting call went. If there is an exception it tells you so you can handle that in your JS otherwise you simply handle the results from the method call. 

    To complete the code on the controller, simply declare a public method in a public class as a remote action and make it static like so:

    @RemoteAction

    public static List<MyObject> methodOnMyController(String firstParemeter, String secondParameter){

    return new List<MyObject>();

    }

    You can refre this link also :

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

    Thanks,

    Debasis