2

Hi folks,
Can anyone tell me how to get the list of account names in visualforce page using java script remoting?
Below is my code

Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.AccController.getAccountList}',

        function(result, event) {
            if (event.status) {
                var ListVariable = '"//I dono how to get the list in javascript"      

            }
        );  

//Controller

@RemoteAction     
 global static List<Account> getAccountList(){     
      List<Account> accList=[select id,Name from account];   
      return accList;    
} 

1 Answer 1

2

That's quite clearly explained in the documentation. Do you understand the code you've written ?

Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.AccController.getAccountList}',
        function(result, event) {
            if (event.status) {
                var ListVariable = '"//I dono how to get the list in javascript"      

            }
        );

The fully qualified remote action is a string that represents the complete path to the remote action method, including namespace, base class, and so on: namespace[.BaseClass][.ContainingClass].ConcreteClass.Method. Use $RemoteAction in an expression to automatically resolve the namespace, for example {!$RemoteAction.MyController.getAccount}.

Invocation parameters are the arguments used to perform the remote method invocation, and are the same arguments used to make a standard remoting call:

  • The parameters to send to the @RemoteAction method, if any.
  • The callback function, which handles the returned result.
  • Configuration details for the invocation, if any.

The second bulletpoint is the answer here, you're written the callback which has 2 parameters :

Your callback function will receive as parameters an event object representing the status of the remote call, and the result object returned by the remote Apex method.

Apex primitive data types returned by result—such as strings or numbers—are converted to their JavaScript equivalents. Apex objects that are returned are converted to JavaScript objects, while collections are converted to a JavaScript array. Keep in mind that JavaScript is case-sensitive, so id, Id, and ID are considered different fields.

So if your remote apex method returns a List<sObject>, the result parameter/variable in your JS will be an JS array containing those objects in JS objects. Basically:

var ListVariable =  result;

So there's also no real need to use an additional variable unless you consider that easier to understand and maintain.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .