+ Start a Discussion
Gautam_KasukhelaGautam_Kasukhela 

How to show Record Type's Name using lightning:dataTable

I am building a custom component to show a list of Cases filtered by a certain criteria. I am using <lightning:dataTable> for this. One of the columns in the list is Record Type and I am unable to display the Name of the Record Type in my list component. It shows up as Id.
 
<lightning:datatable
                             data="{! v.caseData}" 
                              columns="{! v.caseColumns}" 
                              keyField="Id" hideCheckboxColumn="true"/>

//Helper. JS
component.set('v.caseColumns', [
					{label: 'Case Number', fieldName: 'linkName', 
                     type: 'url', 
                     typeAttributes: {label: { fieldName: 'CaseNumber'},target: '_blank'}},
					{label: 'Status', fieldName: 'Status', type: 'text'},
					{label: 'Subject', fieldName: 'Subject', type: 'text'},
                    {label: 'Record Type', fieldName: 'RecordTypeId', type: 'text'},
				]);

//Controller code
listOfCases = [SELECT Id,
                               CaseNumber,
                               Status,
                               RecordType.Name,
                               Subject,
                               OwnerId,
                               CreatedDate
                          FROM Case WHERE xyz...];

In the helper, on the column attributes for Record Type fieldName, I have tried options other than 'RecordTypeId' but that do not show anything in the column. When I run the query in the Console I can see the developer name. What am I missing or doing incorrectly here.

User-added image
Best Answer chosen by Gautam_Kasukhela
Maharajan CMaharajan C
Hi Gautam,

You can use the below approaches for this.

1. Using the Wrapper Class in Apex controller instead of normal Sobject List return: ( Simple Approach)
https://developer.salesforce.com/forums/?id=9060G0000005V95QAE

2.  Using Flatten function in Client side controller:
In the below link they are fetching the Owner Name in table simlarly you have to use Record Type Name.
https://iwritecrappycode.wordpress.com/2017/11/22/salesforce-lightning-datatable-query-flattener/
https://www.wissel.net/blog/2018/08/lightning-datatables-and-relationship-queries.html
https://salesforce.stackexchange.com/questions/200761/parent-field-in-lightning-datatable/200779


Thanks,
Maharajan.C


 

All Answers

Maharajan CMaharajan C
Hi Gautam,

You can use the below approaches for this.

1. Using the Wrapper Class in Apex controller instead of normal Sobject List return: ( Simple Approach)
https://developer.salesforce.com/forums/?id=9060G0000005V95QAE

2.  Using Flatten function in Client side controller:
In the below link they are fetching the Owner Name in table simlarly you have to use Record Type Name.
https://iwritecrappycode.wordpress.com/2017/11/22/salesforce-lightning-datatable-query-flattener/
https://www.wissel.net/blog/2018/08/lightning-datatables-and-relationship-queries.html
https://salesforce.stackexchange.com/questions/200761/parent-field-in-lightning-datatable/200779


Thanks,
Maharajan.C


 
This was selected as the best answer
Gautam_KasukhelaGautam_Kasukhela
Thank you Maharajan for the links. I used the flatten approach and it worked. For other's use, below is the modified code
 
/*Modified the Record Type Column definition*/
component.set('v.caseColumns', [
					{label: 'Case Number', fieldName: 'linkName', 
                     type: 'url', 
                     typeAttributes: {label: { fieldName: 'CaseNumber'},target: '_blank'}},
					{label: 'Status', fieldName: 'Status', type: 'text'},
					{label: 'Subject', fieldName: 'Subject', type: 'text'},
                    {label: 'Record Type', fieldName: 'recordType', 
                     type: 'text', 
                     typeAttributes: {label: { fieldName: 'RecordTypeId'}}}
		]);

/*In the helper file*/
var listOfCases = response.getReturnValue();
 listOfCases.forEach(function(caseRecord){
                       //to create a hyperlink on the case number
                    	caseRecord.linkName = '/'+caseRecord.Id;
                       //to show the record type name 
                         caseRecord.recordType = caseRecord.RecordType.Name;
                					});
component.set("v.caseData", listOfCases);

 
karriche hakimkarriche hakim
Thank you Maharajan for the links. I used the flatten approach and it works for me