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

Retooling from Remote Objects to RemoteAction


I am currently leveraging Remote Objects to populate a javascript datatable in my VF page. The following is the shell code for the table:

j$ = jQuery.noConflict();
j$(document).ready( function () {
    var invoiceTable = j$('[id$="invoicetable"]').DataTable({
        "language": {
        "loadingRecords": "",
        "emptyTable": "No data available"
        // Use the Remote Object retrieve method to get a list of Invoice Payments associated with the Order in question.
        "ajax": function(data, callback, settings) {
            var invoiceData = new SObjectModel.CW_Invoice__x(); //might need offset on limit
            invoiceData.retrieve({where: {Order_Number__c: {eq: "{!OrderNumber}"}}, limit: 100}, function(err, records){      
                if(err) alert(err.message);
 else if (records.length != 0){
                                //process records into dataItems
                        callback({'data': dataItems});     
        // Specify our columns. The first column is used to control expanding and collapsing.
        "columns": [
            { "class": 'details-control',
                "orderable": false,
                "data": null,
                "defaultContent": '',
                width: "8%",
            {"data": "_props.Invoice_Date__c",
                "defaultContent": '' },
            {sClass: "alignRight", "data": ".attribA",
                "defaultContent": '' },
            {sClass: "alignRight", "data": ".attribB",
                "defaultContent": '' },
            {sClass: "alignRight", "data": ".attribC",
                "defaultContent": '' },                            
        order: [[1, 'asc']],
    } );
...code to handle expanding and collapsing, etc.

I need to bring back more than 100 rows so now I want to use a server side @RemoteAction method in the controller to return all records. The following function in my VisualForce page successfully pulls back the relevant data in javascript friendly format: 

function getInvItems(OrderNumberString){ 
function handleInvoiceItems(result, event) { 
           if(event.type == 'exception') { 
          } else { 
         records = result; //records has already been declared as a global variable
} };

I was hoping to update my datatable code to call the above function (as opposed to the remote object retrieve) as follows:

 "ajax": function(data, callback, settings) {
                getInvItems(OrderNumberString); //records has already been declared as a global variable
                if (records.length != 0){

But it seems as though the javascript is moving forward without waiting for the getInvItems function to return values - I get an error saying that it can't return the length of underfined - referring to records.length.

I'm a javscript neophyte so any guidance is very much appreciated.

This is because the getInvItems is an asychronous call and it continues on.  You should instead do all of your record handling your handleInvoiceItems method.