2

I am using datatable plugin in a visualfroce page. Below warning popsup only at the time of page loading and table looks good after closing the alert.

dattaable error

Here is the code...

VisualForce Page

$('#mgtable').dataTable({
           sPaginationType: "full_numbers", "bJQueryUI" : true,"sDom": '<"H"lfr>t<"F"ip>',
           "aoColumnDefs": [
               { "aTargets":[ "mgName" ], "mData": "Name",sClass:"Name", "bAutoWidth": false },
               { "aTargets":[ "gcb" ], "mData": "CreatedBy.Name", sClass:"CBName", "bAutoWidth": false },
               { "aTargets":[ "desc" ], "mData": "Client_Group_Description__c", sClass:"CGDesc", "bAutoWidth": true }
           ],
          "fnInitComplete": function(oSettings) { 
                  MasterDetailListController1.refreshMasterGroupList(function(result, event){
                     if(event.type == 'exception') {
                        alert(event.message);         
                     } else {   
                       $('#mgtable').dataTable().fnAddData(result);         
                     }
                   });
           }

    }); 

    <table id="mgtable" class="display">
        <thead>
            <th class="mgName">Master Group Name</th>
            <th class="gcb">Group Created By</th>
            <th class="desc">Description</th>

        </thead>
        <tbody></tbody>
    </table>

Apex Controller

public with sharing class DataTableController { 

  @RemoteAction
  public static list<Client_Group__c> refreshMasterGroupList() {


    return [select id,Name,CreatedBy.Name,Client_Group_Description__c from Client__c order by Name];     



    }
  • Can you include your vf page code? – Jason Clark Apr 18 '14 at 16:44
  • Does the first row have a null value in it for the column Client_Group_Description__c? I've seen similar errors and it usually had to do with bad data in the column(s) which caused a problem when the plugin serialized the data and stored it in its internal structures. – Mark Pond Apr 18 '14 at 16:47
  • @MarkPond First row has values for all columns but not every row has. – vraavi Apr 18 '14 at 17:15
3

Sounds like your JSON data does not include the Client_Group_Description__c field (in all cases).

  • Does your user have permission to see this field?
  • Can you confirm that the field is in your @RemoteAction response payload on all records returned in the list?

Update: per Josh Kaplan, Salesforce changed the behavior prior to Spring '13 of the JSON serializer to omit fields (properties) which have null values in them, but apparently this change in behavior was reverted back to including the properties which had null values in Spring '13.

If this is indeed still the case, the first option I can think of off the top of my head would be to return your own type rather than returning the sObject type, using an inner class on your controller.

public with sharing class DataTableController { 

    public class ClientGroup {
        public string Id { get; set; }
        public string Name { get; set; }
        public string CreatedByName { get; set; }
        public string ClientGroupDescription { get; set; }

        public ClientGroup(Client__c client) {
            this.Id = client.Id;
            this.Name = client.Name;
            this.CreatedByName = client.CreatedBy.Name;
            this.ClientGroupDescription = client.Client_Group_Description__c;

            // in the event that this still serializes null values and omits the property
            // it can be coded like this:
            //
            //  if (client.Client_Group_Description__c == null) {
            //    this.ClientGroupDescription = '';
            //  } else {
            //    this.ClientGroupDescription = client.Client_Group_Description__c;
            //  }
            //
        }
    }

    @RemoteAction
    public static List<ClientGroup> refreshMasterGroupList() {

        List<ClientGroup> clientGroups = new List<ClientGroup>();

        for (Client__c client : [SELECT Id
                                    , Name
                                    , CreatedBy.Name
                                    , Client_Group_Description__c 
                                FROM Client__c 
                                ORDER BY Name]) {

            // convert to our inner class type
            clientGroups.add(new ClientGroup(client));
        }     

        return clientGroups;

    }
}
  • i am testing as admin.I check log file not all objects in the payload has client_group_description__c field. – vraavi Apr 18 '14 at 18:59
  • It worked after adding the commented code.Thanks Mark. – vraavi Apr 18 '14 at 22:16
  • If you could click the "accept" button on the left side of this answer under the voting buttons, the community would appreciate it. – Mark Pond Apr 18 '14 at 23:03
2

Another way to deal with this is at the client side where if a field that DataTable requires is missing you add it back in as a null:

} else {   
    for (var i = 0; i < result.length; i++) {
        if (result[i].Client_Group_Description__c == undefined) {
            result[i].Client_Group_Description__c = null;
        }
    }
    $('#mgtable').dataTable().fnAddData(result);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.