function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Marco MancillaMarco Mancilla 

Javascript remoting method call returning empty array

Hello,

I am trying to write a simple Apex class with a very simple method that returns a list that I can iterate over in my Javascript without having to resort to using <apex:repeat> and generating a very large document. Unfortunately, I find the documentation on this feature to be confusing and my Javascript is getting an empty array despite my method returning a non-empty array.

Here is my Apex code:
global with sharing class SalesGoalsController {
    public list<Countries__c> CountryRecords {get; set;}
    @RemoteAction
    global static List<Countries__c> getCountryRecords() {
        List<Countries__c>CountryRecords = [select name, psl_goal__c, ps_goal__c, psl_total_goal__c, ps_total_goal__c from Countries__c];
        return CountryRecords;
    }
}
Here is my Visualforce page:
 
<apex:page controller="SalesGoalsController" sidebar="false">
    <script type="text/javascript">
    	Visualforce.remoting.Manager.invokeAction(
            '{!$RemoteAction.SalesGoalsController.getCountryRecords}',
            function(result, event){
                if (event.status) {
                    console.log(result);
                }
            }, {escape: true}
        );
    </script>
</apex:page>
The console.log() call is just showing an empty array in my web browser console. How do I get the list returned by getCountryRecords() into my javascript? I tried JSON.serialize() and parse() but this still doesn't work.

 
Best Answer chosen by Marco Mancilla
Marco MancillaMarco Mancilla
Nevermind, I just realized the object I'm trying to get a list for is empty in this sandbox! Sorry for the confusion!