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
Jonathan Wolff 7Jonathan Wolff 7 

Getting Who.Name into datatable

Hello, I want to get the Who.Name in a datatable, but I struggle with it due to the value not displaying in my datatable component.
I'm quite confident that my query in my apex class is working:
@AuraEnabled
public static List<Task> loadTasks2(Id recordId){
    string userId = UserInfo.getUserId();
    return[SELECT Subject, Who.Name, ActivityDate, Status FROM Task WHERE ActivityDate = TODAY AND OwnerId=:userId AND Status !='Completed'];
}

I think I made a mistake in the Datatable Controller. Could you look in it and tell me what to change, so the Who.Name is shown?

Controller:
component.set('v.mycolumns2', [
            {label: 'Thema', fieldName: 'Subject', type: 'url',
            typeAttributes: {label: { fieldName: 'Subject' }, target: '_blank'}},             
            {label: 'Name', fieldName: 'WhoName', type: 'lookup',
            typeAttributes: {label: { fieldName: 'Who.Name' }, target: '_blank'}},
        ]);
        var action = component.get("c.loadTasks2");
        
        
        
        var whatId = component.get("v.recordId");
        action.setParams({
            "recordId":whatId
        });
          
            action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var records =response.getReturnValue();
                records.forEach(function(record){
                   
                    record.SubjectName = '/'+record.Id;
                    
                });
                component.set("v.tasks2", records);
            }
        });
        $A.enqueueAction(action);

 
ravi soniravi soni
hi Jonathan,
try below code.
<aura:component controller="fetchTaskCtrl">
    <aura:handler name="init" value="{!this}" action="{!c.doInIt}"/>
    <aura:attribute name="data" type="list" />
    <aura:attribute name="columns" type="list" />
      <lightning:datatable
                keyField="id"
                data="{! v.data }"
                columns="{! v.columns }"
                />
	
</aura:component>
----------------------------------------
({
	doInIt : function(component, event, helper) {
		component.set('v.columns', [
            {label: 'Thema', fieldName: 'SubjectName', type: 'url',
            typeAttributes: {label: { fieldName: 'Subject' }, target: '_blank'}},             
            {label: 'Name', fieldName: 'WhoName', type: 'url',
            typeAttributes: {label: { fieldName: 'sWhoName' }, target: '_blank'}},
        ]);
        var action = component.get("c.loadTasks2");
         var whatId = component.get("v.recordId");
        action.setParams({
            "recordId":whatId
        });
          
            action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var records =response.getReturnValue();
                records.forEach(function(record){
                   
                    record.SubjectName = '/'+record.Id;
            record.sWhoName = record.Who != undefined ? record.Who.Name : '';
            record.WhoName = record.Who != undefined ? '/' + record.Who.Id : '';
                });
                component.set("v.data", records);
            }
        });
        $A.enqueueAction(action);
	}
})
--------------------------------
public class fetchTaskCtrl {
    @AuraEnabled
public static List<Task> loadTasks2(Id recordId){
    string userId = UserInfo.getUserId();
    system.debug('userId===> ' + userId);
    return[SELECT Subject, Who.Name,whoId, ActivityDate, Status FROM Task WHERE  OwnerId=:userId AND Status !='Completed'];
}

}

don't forget to mark it as best answer.
thank you
Suraj Tripathi 47Suraj Tripathi 47

Hi Jonathan, 

we can't use fields of Parent object directly during setting fieldname in datatable.

Please try the below code

Apex Controller:

public class loadTasks2 {
    @AuraEnabled
    public static List<taskList> loadTasks(Id recordId){
        try{
            List<taskList> updatedTaskList = new List<taskList>();
            
            taskList obj = new taskList();
            
            string userId = UserInfo.getUserId();
            List<Task> taskLists = new List<Task>();
            taskLists = [SELECT Subject, Who.Name,Who.Id, ActivityDate, Status FROM Task WHERE ActivityDate = TODAY AND OwnerId=:userId AND Status !='Completed' ];
            System.debug('taskLists'+taskLists);
            for(Task taskObj : taskLists){
                obj.Subject = taskObj.Subject;
                obj.WhoName = taskObj.Who.Name;
                obj.WhoId = taskObj.Who.Id;
                obj.TaskId = taskObj.Id;
                
                updatedTaskList.add(obj);
            }
            
            
            System.debug('updatedTaskList'+updatedTaskList);
            return updatedTaskList;
        }
        Catch(Exception ex){
            System.debug('error'+ex.getMessage()+'at line no'+ex.getLineNumber());
            return null;
        }
    }
    
    public class taskList{
        @auraEnabled public String Subject{get;set;}
        @auraEnabled   public String WhoName{get;set;}
        @auraEnabled   public String WhoId{get;set;}
        @auraEnabled  public String TaskId{get;set;}
        
    }
    
}

 

JS Controller:

({
    doInit : function(component, event, helper) {
        component.set('v.columns', [
            {label: 'Thema', fieldName: 'Subject', type: 'String',
            typeAttributes: {label: { fieldName: 'Subject' }, target: '_blank'}},             
            {label: 'Name', fieldName: 'WhoName', type: 'String',
            typeAttributes: {label: { fieldName: 'WhoName' }, target: '_blank'}},
        ]);
        var action = component.get("c.loadTasks");
        
        
        
        var whatId = component.get("v.recordId");
        action.setParams({
            "recordId":whatId
        });
          
            action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var records =response.getReturnValue();
                console.log('response.getReturnValue',JSON.stringify(records));
                component.set("v.data", records);
            }
        });
        $A.enqueueAction(action);


    }
})



I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards.
Suraj Tripathi.

Jonathan Wolff 7Jonathan Wolff 7
Hello,
@veer soni: I tried your code and it did not work, but not I do get a blank space where the name should be.
@suraj: thank you but what I showed you was only a small piece of my code. Changing the code up the way you did it would extend the code dramatically. Is there no was that I display the Who.Name instead of WhoId by adding small pieces to my code. It works absolutely fine for my WhoId
Suraj Tripathi 47Suraj Tripathi 47

Hi Jonathan,

If you want small changes in your code, Please change your Js controller to the given code

Please try the given code

Js Controller:

({
    doInit : function(component, event, helper) {
        component.set('v.columns', [
            {label: 'Thema', fieldName: 'Subject', type: 'String',
             typeAttributes: {label: { fieldName: 'Subject' }, target: '_blank'}},             
            {label: 'Name', fieldName: 'whoName', type: 'String',
             typeAttributes: {label: { fieldName:'whoName'}, target: '_blank'}},
        ]);
            var action = component.get("c.loadTasks");
            
            
            
            var whatId = component.get("v.recordId");
            action.setParams({
            "recordId":whatId
            });
            
            action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
            var records =response.getReturnValue();
            console.log('response.getReturnValue',JSON.stringify(records));
            for(var i=0 ; i<records.length; i++){
            records[i].whoName =  records[i].Who.Name; 
                      }
                      component.set("v.data", records);
    }
});
$A.enqueueAction(action);


}
}) 

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards.
Suraj Tripathi.

ravi soniravi soni
hi Jonathan,
my code is absolute fine because i gave you after validating it but make sure you are able to fetch your data by query. first check your query in query Editor and then try it.
try below query in your query editor and then check you are  getting any data.
SELECT Subject, Who.Name, ActivityDate, Status FROM Task WHERE ActivityDate = TODAY AND OwnerId=:userId AND Status !='Completed'

if you are able to fetch data then I am sure It will help you.
let me know about your status and if it works then mark it as best answer.
Thank you