Salesforce Custom Metadata Types Explained: Benefits, Setup, and Examples

Salesforce's powerful Custom Metadata Types functionality lets developers and administrators make deployable, adjustable configurations and settings. The application cache contains all of the custom metadata, allowing access without requiring repeated database queries.


How to create Custom Metadata Type

1. Search Custom Metadata Type in Setup Quick Find.




2. Click New Custom Metadata Type




3. Fill all required fields and click save





Accessing Custom Metadata Type in Apex


1. Using getAll() - returns map of developer name to metadata type record list. 

Datatype returned by the method - Map<String, Games__mdt>

List<Games__mdt> mcs = Games__mdt.getAll().values();

boolean textField = null;
if (mcs[0].GameType__c == 'PC') {
   textField = true;
}
system.assertEquals(textField, true);


2. Using getInstance(recordId)

Returns a single custom metadata type record sObject for a specified record ID.

Games__mdt mc = Games__mdt.getInstance('m00000000000001');


Using getInstance(developerName) - assume ‘FirstRecord’ is developerName of one record of custom metadata type Games__mdt.

Games__mdt mc = Games__mdt.getInstance('FirstRecord');


Using getInstance(qualifiedApiName)

The namespace prefix and developerName are concatenated to generate the qualifiedApiName, which has the following format: namespacePrefix__developerName.

Games__mdt mc = Games__mdt.getInstance('MyNamespace__FirstRecord');


Limitations with Custom Metadata Type

1. Longer text fields are truncated in custom metadata type records because only the first 255 characters are returned for every field if you use getAll(), getInstance(recordId), getInstance(developerName) method. Use a SOQL query to retrieve all the field data from a custom metadata type record.

Comments

Popular posts from this blog

How to Add a Dynamic Child List to Parent in Apex Class || Trigger using map

A - Z Guide to Using the Wire Decorator in LWC Salesforce

(Salesforce Apex 2024 Release) getSalesforceBaseUrl method is deprecated