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 

Problem when having Who.Name field in datatable and record with empty field


Hello, I have build a task datatable component and added the Who.Name as a column. It is working fine when the Who.Name field has content in it, otherwise I get an error message when leaving the field empty. Could you give me a solution for it?

User-added image


My code:

APEX
//ÜBERFÄLLIG - Abfrage Tasks, wo das Activity Date abgelaufen und der Status "Not Completed" ist//
@AuraEnabled
public static List<Task> loadTasks(Id recordId){
    string userId = UserInfo.getUserId();
    return[SELECT Subject, Who.Name, WhoId, ActivityDate, Status FROM Task WHERE ActivityDate< TODAY AND OwnerId=:userId AND Status !='Completed'];
}


JS
 
component.set('v.mycolumns', [
            {label: 'Thema', fieldName: 'SubjectName', type: 'url',
            typeAttributes: {label: { fieldName: 'Subject' }, target: '_blank'}},
                 {label: 'Name', fieldName: 'whoName', type: 'text',
            typeAttributes: {label: { fieldName: 'Who.Name' }, target: '_blank'}},
        ]);
        var action = component.get("c.loadTasks");
            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.whoName = record.Who.Name
                });
                component.set("v.tasks", records);
            }
        });
        $A.enqueueAction(action);

 
Best Answer chosen by Jonathan Wolff 7
Maharajan CMaharajan C
Hi Jonathan,

Please try the below update:

record.whoName = (record.WhoId != '' && record.WhoId != null) ? record.Who.Name : '';
 
component.set('v.mycolumns', [
            {label: 'Thema', fieldName: 'SubjectName', type: 'url',
             typeAttributes: {label: { fieldName: 'Subject' }, target: '_blank'}},
            {label: 'Name', fieldName: 'whoName', type: 'text',
             typeAttributes: {label: { fieldName: 'Who.Name' }, target: '_blank'}},
        ]);
        
            var action = component.get("c.loadTasks");
            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.whoName = (record.WhoId != '' && record.WhoId != null) ? record.Who.Name : '';
            });
            component.set("v.tasks", records);
            }
            });
            $A.enqueueAction(action);

Thanks,
Maharajan.C

All Answers

mukesh guptamukesh gupta
Hi Jonathan,

Please update your code line with
 
component.set('v.mycolumns', [
            {label: 'Thema', fieldName: 'SubjectName', type: 'url',
            typeAttributes: {label: { fieldName: 'Subject' }, target: '_blank'}},
                 {label: 'Name', fieldName: 'whoName', type: 'text',
            typeAttributes: {label: { fieldName: 'Who.Name' }, target: '_blank'}},
        ]);
        var action = component.get("c.loadTasks");
            action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var records =response.getReturnValue();
                records.forEach(function(record){
                   
                    record.SubjectName = '/'+record.Id;
                    if(record.Who.Name  != ''){
                             record.whoName = record.Who.Name

                      }
                });
                component.set("v.tasks", records);
            }
        });
        $A.enqueueAction(action);

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Jonathan Wolff 7Jonathan Wolff 7
Hi mukesh, by using your code I still get error when the Name field in Tasks is empty :(

User-added image
Maharajan CMaharajan C
Hi Jonathan,

Please try the below update:

record.whoName = (record.WhoId != '' && record.WhoId != null) ? record.Who.Name : '';
 
component.set('v.mycolumns', [
            {label: 'Thema', fieldName: 'SubjectName', type: 'url',
             typeAttributes: {label: { fieldName: 'Subject' }, target: '_blank'}},
            {label: 'Name', fieldName: 'whoName', type: 'text',
             typeAttributes: {label: { fieldName: 'Who.Name' }, target: '_blank'}},
        ]);
        
            var action = component.get("c.loadTasks");
            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.whoName = (record.WhoId != '' && record.WhoId != null) ? record.Who.Name : '';
            });
            component.set("v.tasks", records);
            }
            });
            $A.enqueueAction(action);

Thanks,
Maharajan.C
This was selected as the best answer
Jonathan Wolff 7Jonathan Wolff 7
Hi Maharajan, I tried your code sample but I still got the error:
"Action.prototype.finishAction Error  [Error in $A.getCallback() [Cannot set properties of undefined (setting 'Name')]
Callback failed: apex://TaskController/ACTION$loadTasks]
"

What can I do about it?
Maharajan CMaharajan C
Please post your recent code... Because in my org the above code is working...
Jonathan Wolff 7Jonathan Wolff 7
You were right, I had forgotten to insert sth to my js. Thank you so much for the help. Could you maybe tell me what I have to do so I do get the name and can click on it to lead to the record of him? 
Maharajan CMaharajan C
Please try the below change:
 
({
    init: function (component, event, helper) {
        component.set('v.mycolumns', [
            {label: 'Thema', fieldName: 'SubjectName', type: 'url',
             typeAttributes: {label: { fieldName: 'Subject' }, target: '_blank'}},
            {label: 'Name', fieldName: 'whoName', type: 'url',
             typeAttributes: {label: { fieldName: 'Who_Name' }, target: '_blank'}},
        ]);
        
            var action = component.get("c.loadTasks");
            action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
            var records =response.getReturnValue();
            records.forEach(function(record){
            record.SubjectName = '/'+record.Id;
            if(record.WhoId != '' && record.WhoId != null){
            	record.Who_Name =  record.Who.Name;
            	record.whoName = '/' + record.WhoId;
            }
            });
            component.set("v.tasks", records);
            }
            });
            $A.enqueueAction(action);
    }
})

Thanks,
Maharajan.C
Jonathan Wolff 7Jonathan Wolff 7
Hi Maharajan, by using your code I do get the record.Id with the link instead of the Who.Name. How can I change that so I get the name instead of the Id with the same link?

Greetings and thank you :)
Maharajan CMaharajan C
Hi Jonathan,

Do you want the name should be displayed in the lead name hover instead of Id? I don't think it's simple you have to think and work... there is no need of name in the url hover because already we are displaying the name in the table column ... Do you have any other issue to discuss... Otherwise please close this threat by choosing the best answer...

For me the table looks like below:

User-added image

Thanks,
Maharajan.C