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
Phuc Nguyen 18Phuc Nguyen 18 

Custom Medadata Type in Aura component

Currently have an aura component where the Salesforce object is hard coded in the cmp and design file.

Is there a way to use a CMT set by the user to determine object?  So if the cmt value is Contact pass in Contact or if Account pass in Account and so on.

I know I can create a controller to query the cmt but now sure how to utilize it in the component
 
@AuraEnabled
        public static List < File_Object__mdt > fetchObject() {
            return [ SELECT object__c FROM file_Object__mdt ];
        }

 
Best Answer chosen by Phuc Nguyen 18
AnudeepAnudeep (Salesforce Developers) 
I recommend looking at the following example 

Apex class:
 
public class USStateCodeController {
 
    @AuraEnabled
    public static List < US_State_Codes__mdt > fetchUSStateCodes() {
        return [ SELECT Label, State_Code__c FROM US_State_Codes__mdt ORDER BY Label ];
    }
}

Component: 
 
<aura:component implements="force:appHostable" controller="USStateCodeController" >
    
    <aura:handler name="init" value="{!this}" action="{!c.fetchCodes}"/>
    
    <aura:attribute name="codeList" type="US_State_Codes__mdt[]" default=""/>
    <aura:attribute name="mycolumns" type="List"/>
    
    <div class="slds-box slds-theme_default">
        <lightning:datatable data="{! v.codeList }" 
                             columns="{! v.mycolumns }" 
                             keyField="id"
                             hideCheckboxColumn="true"/>
    </div>
 
</aura:component>

Controller: 
 
({
 fetchCodes : function(component, event, helper) {
         component.set('v.mycolumns', [
            {label: 'Label', fieldName: 'Label', type: 'text'},
                {label: 'State Code', fieldName: 'State_Code__c', type: 'text'}
            ]);
        var action = component.get("c.fetchUSStateCodes");
        action.setParams({
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.codeList", response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})

If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!

Anudeep
 

All Answers

AnudeepAnudeep (Salesforce Developers) 
I recommend looking at the following example 

Apex class:
 
public class USStateCodeController {
 
    @AuraEnabled
    public static List < US_State_Codes__mdt > fetchUSStateCodes() {
        return [ SELECT Label, State_Code__c FROM US_State_Codes__mdt ORDER BY Label ];
    }
}

Component: 
 
<aura:component implements="force:appHostable" controller="USStateCodeController" >
    
    <aura:handler name="init" value="{!this}" action="{!c.fetchCodes}"/>
    
    <aura:attribute name="codeList" type="US_State_Codes__mdt[]" default=""/>
    <aura:attribute name="mycolumns" type="List"/>
    
    <div class="slds-box slds-theme_default">
        <lightning:datatable data="{! v.codeList }" 
                             columns="{! v.mycolumns }" 
                             keyField="id"
                             hideCheckboxColumn="true"/>
    </div>
 
</aura:component>

Controller: 
 
({
 fetchCodes : function(component, event, helper) {
         component.set('v.mycolumns', [
            {label: 'Label', fieldName: 'Label', type: 'text'},
                {label: 'State Code', fieldName: 'State_Code__c', type: 'text'}
            ]);
        var action = component.get("c.fetchUSStateCodes");
        action.setParams({
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.codeList", response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})

If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!

Anudeep
 
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
Thanks for the reply Anudeep
Do you know how I can reference the mdt value in the Design file?  I am getting an error.
bids\main\default\aura\FileShare\FileShare.cmp  The flexipage:availableForRecordHome interface doesn't support these attribute types in the design resource: file_Object__mdt []
The new custom field in the mdt is a text field