Showing posts with label Custom Metadata Type. Show all posts
Showing posts with label Custom Metadata Type. Show all posts

Wednesday, December 16, 2020

Access Custom Metadata Type data without Using the SOQL in Salesforce

From Spring'21 onwards we can get the custom metadata record information without using the SOQL.

Demo:

For the demo, I created the Country and Amount(Country_And_Amount__mdt) Custom Metadata type object and records.

Country_And_Amount__mdt object.


Sample Code:

// Getting all metadata values 
Map<String, Country_And_Amount__mdt> mapValues = Country_And_Amount__mdt.getAll();

for(Country_And_Amount__mdt mdt : mapValues.values()) {
    System.debug('Name => '+mdt.Label);
    System.debug('DeveloperName => '+mdt.DeveloperName);
    System.debug('Amount__c => '+mdt.Amount__c);
}


// Getting metadata record based on value
Country_And_Amount__mdt objMdt = Country_And_Amount__mdt.getInstance('United_States');
System.debug('objMdt => '+objMdt);


Output:



Tuesday, December 17, 2019

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

What is Custom Metadata Type in Salesforce?
Custom metadata are like custom settings but records in custom metadata type considered as metadata rather than data.
These are typically used to define application configurations that need to be migrated from one environment to another, or packaged and installed.

You cannot perform CUD (Create, Update, Delete) operation on custom metadata type in apex.

Custom Metadata Type object suffix with __mdt

Normally in apex, we are using SOQL query to get the Custom metadata type data.

How to get the Custom Metadata Type data in Lightning web components?

To get the Custom metadata type data in the lightning web component you can use the wire service to get record data. To get the data, the Lightning Web Component uses the getRecord wire adapter.

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

For the demo, I created the Country and Amount(Country_And_Amount__mdt) Custom Metadata type object and records.

Country_And_Amount__mdt object


Country_And_Amount__mdt records



HTML Code
<template>
    <lightning-card title="Custom Metadata Types Record Values">
        <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">Amount:</dt>
                <dd class="slds-item_detail slds-truncate"><b>{objMetadataValues.Amount}</b></dd>
            </dl>
        </template>
    </lightning-card>
</template>

Javascript Controller
import { LightningElement, track, wire} from 'lwc';

// import to get the record details based on record id
import { getRecord } from 'lightning/uiRecordApi';

export default class metadataDetailsInLWC 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: ['Country_And_Amount__mdt.MasterLabel', 'Country_And_Amount__mdt.DeveloperName', 'Country_And_Amount__mdt.Amount__c'] })
    userData({error, data}) {
        if(data) {
            let currentData = data.fields;
            this.objMetadataValues = {
                MasterLabel : currentData.MasterLabel.value,
                DeveloperName: currentData.DeveloperName.value,
                Amount: currentData.Amount__c.value

            }
        } 
        else if(error) {
            window.console.log('error ====> '+JSON.stringify(error))
        } 
    }

}

Output

Monday, November 18, 2019

How to deploy custom metadata type using ANT tool in Salesforce

Use the below package.xml to retrieve/deploy the custom metadata type using ANT tool
Custom metadata type is like custom object but you have use __mdt
Ex: custommetdatatypename__mdt

package.xml
<!-- Custom Metadata Type -->  
<types>  
    <members>CustomMetadataAPIname__mdt</members>  
    <name>CustomObject</name>  
</types>  

<!-- Custom Metadata Type records/data-->  
<types>  
    <members>CustomMetadata__mdt.RecordName</members>  
    <name>CustomMetadata</name>  
</types>  
 


Thursday, May 2, 2019

How to use custom metadata type values in Process Builder

In the Summer 19 release, Salesforce introduced the option to use custom metadata type values in process builder, formulas, and validation rules.

if you don't have pre-release dev org, sign up using below link
https://www.salesforce.com/form/signup/prerelease-summer19/

Syntax to use Metadata value in Formulas
$CustomMetadata.CustomMetadataTypeAPIName.RecordAPIName.FieldAPIName

Example
I created the Account_Generic_Data__mdt for this demo.

and created the Value__c filed on Account Generic Data Custom metadata type.
and I created the record

To Use this value in formulas and process builders
Example:
$CustomMetadata.Account_Generic_Data__mdt.Acc_Minimum_Amount.Value__c 

Resource:
https://releasenotes.docs.salesforce.com/en-us/summer19/release-notes/rn_forcecom_development_custom_metadata_process_builder.htm?edition=&impact=