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
Andrew Aldis 15Andrew Aldis 15 

Lightning:datatable not displaying records

I am trying to create a data table using the Lightning Datatable component and cannot seem to get the values to bind to the table.  The values are coming from a wrapper class on the controller that is pulling the information correctly and I can see the values I want in the console log of the test page, but they never populate in the table itself.  Please see my code below.  Any help would be appreciated.

Controller:

@auraEnabled
public static List<allActivityWrapper> getAllActivities(string parentId) {
  set<Id> taskRelationIds = new set<Id>();
  set<Id> eventRelationIds = new set<Id>();
system.debug('parent Id is '+parentId);
  set<taskRelation> taskRelations = new set<taskRelation>();
  set<eventRelation> eventRelations = new set<eventRelation>();
    List<taskRelation> tskRel = [select taskId, relation.Name from taskRelation where accountId = :parentId and isWhat = false order by createdDate asc];
    for(taskRelation tr: tskRel) {
      taskRelationIds.add(tr.taskId);
      taskRelations.add(tr);
    }
    system.debug('taskRelations list is '+taskRelations.size());
    List<eventRelation> evtRel = [select eventId, relation.Name from eventRelation where accountId = :parentId and isInvitee = false order by createdDate asc];
    for(eventRelation ev: evtRel) {
      eventRelationIds.add(ev.eventId);
      eventRelations.add(ev);
    }
    system.debug('eventRelations list is '+eventRelations.size());
    Id usrId = UserInfo.getUserId();
    string taskQuery = 'SELECT Id, Activity_Type__c, CreatedDate,  who.Name, Meeting_Type__c, Subject, Short_Description__c, ActivityDate, Description, Activity_Status__c, Owner.Name, Financial_Account__c, ActivityRelation__c, WhoId, Who.FirstName, Who.LastName  FROM task WHERE (Firm__c = :parentId OR whatId = :parentId or Id in :taskRelationIds) order by ActivityDate Desc';
    System.debug('****** task query is'+taskQuery);
    string eventQuery = 'SELECT Id, Activity_Type__c,recordType.name, who.Name, CreatedDate, Meeting_Type__c, Subject, Short_Description__c, StartDateTime, Description, Activity_Status__c, Owner.Name,  Financial_Account__c, ActivityRelation__c, WhoId, Who.FirstName, Who.LastName  FROM event  WHERE (Firm__c = :parentId OR whatId = :parentId or Id in :eventRelationIds) order by StartDateTime Desc';
    System.debug('****** event query is'+eventQuery);
    integer tableList = 750;
    string contactName = '';
    List<allActivityWrapper> allActivityList = new List<allActivityWrapper>();

    List<Task> taskList = Database.Query(taskQuery);
    List<event> eventList = Database.Query(eventQuery);
    System.debug('****** task list size is '+taskList.size());
    System.debug('****** event list size is '+eventList.size());
    integer totalListSize = taskList.Size() + eventList.Size() ;
    if(totalListSize < tableList){
      tableList = totalListSize;
    }
    system.debug('allActivityList 1 is '+allActivityList.size());
    for(task t:taskList){
      if(t.whoId == Null){
        contactName = 'No Contact';
      }else{
        contactName = t.who.name;
      }
      allActivityList.add(new allActivityWrapper(t, contactName));
    }
    system.debug('allActivityList 2 is '+allActivityList.size());
     Integer i = 0;
     for(event e:eventList){
       while (i < allActivityList.size()) {
         system.debug('event is '+e.StartDateTime +' task is '+ allActivityList[i].actCompareDate);
                   if (e.StartDateTime > allActivityList[i].actCompareDate) {
                     system.debug('event being entered is '+e.Id);
                     if(e.whoId == Null){
                       contactName = 'No Contact';
                     }else{
                       contactName = e.who.name;
                     }
                       allActivityList.add(i, new allActivityWrapper(e, contactName ));
                       break;
                   }
               i++;
          }
       }
       system.debug('allActivityList 3 is '+allActivityList.size());
    for(allActivityWrapper a: allActivityList){
      system.debug('Activity List Date is '+a.actDueDate+' name is '+a.actPrimaryContact+' id is '+a.actId);
    }
    return allActivityList;
}

public class allActivityWrapper{
    @AuraEnabled public string actId {get; set;}
    @AuraEnabled public string actLink {get; set;}
    @AuraEnabled public string actType {get; set;}
    @AuraEnabled public string actSubType {get; set;}
    @AuraEnabled public string actSubject {get; set;}
    @AuraEnabled public string actAssigned {get; set;}
    @AuraEnabled public string actFullDesc {get; set;}
    @AuraEnabled public string actPrimaryContact {get; set;}
    @AuraEnabled public string actDueDate {get; set;}
    @AuraEnabled public datetime actCompareDate {get; set;}
    @AuraEnabled public string actOwner {get; set;}

    public allActivityWrapper(Task awTask, string contactName){
        actId = awTask.Id;
        actLink ='View';
        actType = awTask.Activity_Type__c;
        actSubType  = awTask.Meeting_Type__c;
        actSubject = awTask.Subject;
        actDueDate = String.ValueOf(awTask.CreatedDate);
        actCompareDate = awTask.ActivityDate;
        actAssigned = awTask.Owner.Name;
        actFullDesc  = awTask.Short_Description__c;
        actPrimaryContact = contactName;
        actOwner = awTask.Owner.Name;
    }

    public allActivityWrapper(event awEvent, string contactName){
        actId = awEvent.Id;
        actLink ='View';
        actType = awEvent.Activity_Type__c;
        actSubType  = awEvent.Meeting_Type__c;
        actSubject = awEvent.Subject;
        actDueDate = String.ValueOf(awEvent.StartDateTime);
        actCompareDate = awEvent.StartDateTime;
        actAssigned = awEvent.Owner.Name;
        actFullDesc  = awEvent.Short_Description__c;
        actPrimaryContact = contactName;
        actOwner = awEvent.Owner.Name;
    }
}

Lightning Component:

<aura:component controller="ActivityFirmComponentControllerSCRewrite">
    <aura:handler name="init" value="{!this}" action="{!c.init}" />
    <aura:attribute name="actData" type="Object" access="GLOBAL" description="The page data"/>
    <aura:attribute name="actColumns" type="List" description="The data to be displayed in the grid."/>
    <aura:attribute name="parentId" type="String" description="The Firm Id to be passed to the query." />

    <div>
        <lightning:datatable data="{!v.actData}"
                             columns="{!v.actColumns}"
                             keyField="id"
                             hideCheckboxColumn="true"/>
    </div>
    
</aura:component>

Lightning Component Controller: 

({
    init: function(component, event, helper) {
      component.set('v.actColumns', [
        {label: 'DUE DATE',fieldName: 'actDueDate',type: 'dateTime'},
        {label: 'PRIMARY CONTACT',fieldName: 'actPrimaryContact',type: 'text'},
        {label: 'ACTIVITY TYPE',fieldName: 'actType',type: 'text'},
        {label: 'SUBTYPE',fieldName: 'actSubType',type: 'text'},
        {label: 'DESCRIPTION',fieldName: 'actFullDesc',type: 'text'},
        {label: 'SUBJECT',fieldName: 'actSubject',type: 'text'},
        {label: 'ASSIGNED TO',fieldName: 'actAssigned',type: 'text'},
        {label: 'VIEW',fieldName: 'actLink',type: 'text'}
      ]);
         
          
           helper.getAllActivities(component, event);
          }

})


Lightning Component Helper:

({
    getAllActivities : function(component, event) {
        var action = component.get("c.getAllActivities");
        var parentId = component.get("v.parentId");
        var returnJson;
        var returnValue;
        var state;
        action.setParams({
            parentId : '001m000000box8NAAQ'
        })
        action.setCallback(this, function(response){
            state = response.getState();
            console.log('state is '+state);
            console.log('action is '+action);
            if(state === 'SUCCESS'){
                returnJson = response.getReturnValue();
                console.log('***** response returnJson ' + returnJson.length);
                if (returnJson.length > 0) {
                    console.log('***** response returnJson actId is  ' + returnJson[0].actId);
                    console.log('***** response returnJson actDueDate is  ' + returnJson[0].actDueDate);
                    console.log('***** response returnJson actPrimaryContact is  ' + returnJson[0].actPrimaryContact);
                    console.log('***** response returnJson actType is  ' + returnJson[0].actType);
                    console.log('***** response returnJson actSubType is  ' + returnJson[0].actSubType);         
                    console.log('***** response returnJson actFullDesc is  ' + returnJson[0].actFullDesc);
                    console.log('***** response returnJson actSubject is  ' + returnJson[0].actSubject);
                    console.log('***** response returnJson actAssigned is  ' + returnJson[0].actAssigned);
                    console.log('***** response returnJson actLink is  ' + returnJson[0].actLink);                 
                    component.set("v.actColumns", returnJson);
                    console.log('***** after component set  ');
                } 
            }else if (state === 'ERROR'){
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " +errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }else{
                console.log('Something went wrong, Please check with your admin');
            }
        });
        $A.enqueueAction(action);    
        
    }
})
Best Answer chosen by Andrew Aldis 15
Anil SomasundaranAnil Somasundaran
Hi Andrew,
It will work if you assign the "returnJson" value to "actData" attribute of the component.  Change the component.set code in the getAllActivities helper method like below.
 
component.set("v.actData", returnJson);
Thanks,
Anil 
Please mark this as best answer and upvote if your query has been resolved. Visit my blog to find more about lightning https://techevangel.com/author/anilsomasundaran/   (http://techevangel.com/author/anilsomasundaran/)