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
Emily Johnson 23Emily Johnson 23 

Flattening custom Metadata to get relationship field name in lightning DataTables

I'm using a datatable in a lightning (aura) component. Since the columns don't allow for notation to get the relationship_field__r.QualifiedApiName I am trying to flatten the data the way you would with a lookup field on an object (as referenced here: https://salesforce.stackexchange.com/questions/200761/parent-field-in-lightning-datatable)

However it is not working and my column is still displaying blank.

Has anyone had success with a similar requirement displaying custom metadata relationship field names in a datatable?

Best Answer chosen by Emily Johnson 23
Rushikesh KhandaleRushikesh Khandale
Hello,
I implementated the aura component with flattening Custom metadata. Please refer the following code which worked for me.
Aura Component:
<aura:component implements="flexipage:availableForAllPageTypes" Controller="GetCustomMetadata">

     <!-- This attribute saves the record ID -->
     <aura:attribute name="mydata" type="Object"/>
     <aura:attribute name="mycolumns" type="List"/>
     <aura:attribute name="recordLoadError" type="String"/>
     <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

     <lightning:datatable data="{! v.mydata }" 
        columns="{! v.mycolumns }" 
        keyField="Id"/>

</aura:component>
JS Function:
doInit : function(component, event, helper) {
        var action = component.get('c.getServiceProviders');
        component.set('v.mycolumns', [
            {label: 'Object Name', fieldName: 'ObjectName', type: 'text'},
            {label: 'Master Label', fieldName: 'MasterLabel', type: 'text'}
        ]);
        action.setCallback(this, $A.getCallback(function (response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var rows = response.getReturnValue();
                for(var i = 0 ; i < rows.length;i++) {
                    var row = rows[i];
                    row.ObjectName = row.Object_Type__r.DeveloperName;
                }
                component.set('v.mydata', rows);
            } else if (state === "ERROR") {
                var errors = response.getError();
            }
        }));
        $A.enqueueAction(action);
    }
Apex Controller:
public with sharing class GetCustomMetadata {
    @AuraEnabled
    public static List<Service_Provider__mdt> getServiceProviders(){
        return [
            SELECT
                MasterLabel,
                Object_Type__r.DeveloperName
            FROM
                Service_Provider__mdt
        ];
    }
}

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
 

All Answers

Rushikesh KhandaleRushikesh Khandale
Hello,
I implementated the aura component with flattening Custom metadata. Please refer the following code which worked for me.
Aura Component:
<aura:component implements="flexipage:availableForAllPageTypes" Controller="GetCustomMetadata">

     <!-- This attribute saves the record ID -->
     <aura:attribute name="mydata" type="Object"/>
     <aura:attribute name="mycolumns" type="List"/>
     <aura:attribute name="recordLoadError" type="String"/>
     <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

     <lightning:datatable data="{! v.mydata }" 
        columns="{! v.mycolumns }" 
        keyField="Id"/>

</aura:component>
JS Function:
doInit : function(component, event, helper) {
        var action = component.get('c.getServiceProviders');
        component.set('v.mycolumns', [
            {label: 'Object Name', fieldName: 'ObjectName', type: 'text'},
            {label: 'Master Label', fieldName: 'MasterLabel', type: 'text'}
        ]);
        action.setCallback(this, $A.getCallback(function (response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var rows = response.getReturnValue();
                for(var i = 0 ; i < rows.length;i++) {
                    var row = rows[i];
                    row.ObjectName = row.Object_Type__r.DeveloperName;
                }
                component.set('v.mydata', rows);
            } else if (state === "ERROR") {
                var errors = response.getError();
            }
        }));
        $A.enqueueAction(action);
    }
Apex Controller:
public with sharing class GetCustomMetadata {
    @AuraEnabled
    public static List<Service_Provider__mdt> getServiceProviders(){
        return [
            SELECT
                MasterLabel,
                Object_Type__r.DeveloperName
            FROM
                Service_Provider__mdt
        ];
    }
}

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
 
This was selected as the best answer
Emily Johnson 23Emily Johnson 23
Rushikesh Khandale,

Thanks for your help. All those comments and I think I was just having a caching issue ("shrug"). It is working fine now.
Rushikesh KhandaleRushikesh Khandale
No worries....Happy to help!