[SalesForce] Return string value of id on jQuery datatable server side sorting

Hi I'm working on a server-side sorting using jQuery datatables based on an example from here. Everything works fine bar one detail – the look up fields to the user returns the user id and I'm not sure how to change it to the actual string value. I know visualforce natively handles this but in this case I'm not exactly rendering the table with visualforce. Here is a sample of what I have:

<apex:page controller="DataTableController">



<apex:sectionHeader title="DataTables"/>


  <table id="table" cellpadding="0" cellspacing="0" border="0">
       <thead>
          <th>Case Number</th>
          <th>Date/Time Opened</th>
          <th>Requested By</th>
          <th>Subject</th>
          <th>Release Type</th>
          <th>Status</th>
          <th>Priority</th>
          <th>Major Release Version</th>
          <th>Case Owner</th>
          <th>Type</th>
          <th>Created By</th>

          </thead>
          <tbody>

          </tbody>
  </table>




        <apex:includeScript value="{!URLFOR($Resource.jquery, 'js/jquery-1.9.1.min.js')}"/>
        <script type="text/javascript" language="javascript" src="{!URLFOR($Resource.jQueryDataTablesZip, 'jQueryDataTablesZip/js/jquery.dataTables.min.js')}"></script>            
        <apex:stylesheet value="{!URLFOR($Resource.jQueryDataTablesZip, 'jQueryDataTablesZip/css/jquery.dataTables.css')}"/>

        <script>

        var j$ = jQuery.noConflict();

        var fields = ['CaseNumber','CreatedDate','Requested_By__c' ,'Subject','Release_Type__c','Status','Priority','Major_Release_version__c', 'OwnerId', 'Type','CreatedById'];

        var aoColumns = [];
        for (var i = 0; i < fields.length; i++) {
            aoColumns.push({'mData': fields[i]});
        }

        j$(document).ready(function() {
            j$('#table').dataTable({
                'aoColumns': aoColumns,
                'bProcessing': true,
                'bServerSide': true,
                'bFilter': true,
                'sAjaxSource': 'https://my.salesforceURL.com',
                'fnServerData': function(sSource, aoData, fnCallback) {
                    console.log(JSON.stringify(aoData));
                    // Call the @RemoteAction JavaScript function
                    DataTableController.Cases(aoData, function(result, event) {
                        if (event.type != 'exception') {
                            console.log(JSON.stringify(result));
                            for (var i = 0; i < result.aaData.length; i++) {
                                var r = result.aaData[i];
                                console.log('this is r value: '+r);
                                for (var j = 0; j < fields.length; j++) {
                                    var field = fields[j];
                                    console.log('this is field key :' + field);
                                    console.log('this is field value :' + r[field]);
                                    if (r[field] == undefined) {
                                        // DataTables pops a dialog for undefined values
                                        r[field] = null;
                                    } else if (field == 'CreatedDate') {
                                        // Dates transmitted as longs
                                        var d = new Date(r[field]);
                                        r[field] = ''
                                                + (d.getMonth() + 1)
                                                + '/'
                                                + d.getDate()
                                                + '/'
                                                + d.getFullYear()
                                                ;
                                    }
                                }
                            }
                            // Call back into the DataTable function
                            fnCallback(result);
                        } else {
                            alert(event.message);
                        }
                    });
                }
            });
        });

        </script>                                              

Thanks for any and all suggestions.

Best Answer

Of the three Id fields that I see, and there may be more I'm missing, you would need to query for these instead to get back the associated record names:

  • Requested_By__r.Name
  • Owner.Name
  • CreatedBy.Name