Showing posts with label Custome Meta data. Show all posts
Showing posts with label Custome Meta data. Show all posts

Thursday, June 3, 2021

How to Access Custom Metadata Type Records Using Static Methods

Custom Metadata Type Records Using Static Methods 

We have now new methods for Custom Metadata Types as like Custom Settings. by using these method we can remove the SOQL  statement to get Custom Metadata Type Records.

 before Sprint 21:-how we access the Custom Metadata Type records

list<Test_mdt> lstMetaDataRecords = [Select Id, MasterLabel from Test_mdt]

 After Sprint 21:-how we access the Custom Metadata Type records ,

 getAll() -Will return the all records from Custom Metadata Type.

Map<String, Test__mdt> mapMetaDataRecords = Test__mdt.getAll();

list<Test_mdt> lstMetaDataRecords = mapMetaDataRecords.values();

getInstance() – Will return a record from Custom Metadata Type.

Test_mdt objMetaDataRecord= Test_mdt.getInstance('test');

Monday, October 26, 2020

How to Get Custom Metadata Type data Lightning Web Components(lwc) without Apex

Hello All,

Today we will talk about Custom Metadata type and how to get Cusotm Metadata Type details into to LWC without writing any apex code.

Detail about Custom Metadata type:-

1) Custom metadata are like custom settings but records in custom metadata type 
    considered as metadata rather than data.
2) Custom MetaData are mostly used to define application configurations that need to be  migrated from one environment to another, or packaged and installed.
3)we can not perform CUD Create, Update, Delete) operation on custom metadata 
   type in apex.

AS we know in Custom Object suffix is __C  but for Custom Metadata type we are using __mdt


How to Get Custom Metadata Type data Lightning Web Components(lwc) without Apex


To Achive this we can use Wire service to get record data, the Lightning Web Component uses the getRecord wire adapter.

Syntax
import { getRecord } from 'lightning/uiRecordApi';

Sample Code:-
1) create Custom Metadata type:-
2) Get into LWC

 create Custom Metadata type:

Get into LWC

myMetadata.html
    <lightning-card title="Custom Metadata Types Record Values" icon-name="standard:contact">
        <template if:true={objMetadataValues}>
            <dl class="slds-list_horizontal slds-wrap" style="margin-left: 3%;">
                <dt class="slds-item_label slds-truncate" title="First Name">Master Label</dt>
                <dd class="slds-item_detail slds-truncate"><b>{objMetadataValues.MasterLabel}</b>                    </dd>
                <dt class="slds-item_label slds-truncate" title="Last Name">Developer Name:</dt>
                <dd class="slds-item_detail slds-truncate"><b>{objMetadataValues.DeveloperName}                </b></dd>
                <dt class="slds-item_label slds-truncate" title="Full Name">Active:</dt>
                <dd class="slds-item_detail slds-truncate"><b>{objMetadataValues.Active}</b>                        </dd>
            </dl>
        </template>
    </lightning-card>
</template>

myMetadata.js

import { LightningElement, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
const FIELDS = [
    'MY_Account_Data__mdt.MasterLabel',
    'MY_Account_Data__mdt.DeveloperName',
    'MY_Account_Data__mdt.isActive__c',
    'MY_Account_Data__mdt.AccoutType__c',
];
export default class myMetadata extends LightningElement {   
  @track objMetadataValues = {};
    
    // using wire service getting current user data
    // HardCoded the Record Id you can dynamically pass the record id using track or api decorates
    @wire(getRecord, { recordId: 'm02B00000006hGx', fields: FIELDS })
    userData({error, data}) {
        if(data) {
            let currentData = data.fields;
            this.objMetadataValues = {
                MasterLabel : currentData.MasterLabel.value,
                DeveloperName: currentData.DeveloperName.value,
                Active: MY_Account_Data__mdt.AccoutType__c,
                   AccountType:MY_Account_Data__mdt.AccoutType__c
         
            }
        } 
        else if(error) {
            window.console.log('error ====> '+JSON.stringify(error))
        } 
    }
}

Output