Home SALESFORCERelease Spring 21 – Access Custom Metadata Type Records using SOQL and without Using SOQL

Spring 21 – Access Custom Metadata Type Records using SOQL and without Using SOQL

Custom metadata types enable you to create your own setup objects whose records are 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.

Rather than building apps from data records in custom objects or custom settings, you can create custom metadata types and add metadata records, with all the manageability that comes with metadata: package, deploy, and upgrade. Querying custom metadata records doesn’t count against SOQL limits.

 

Before Spring 21 release we have the option to access Custom Metadata Type Records only Using SOQL likely below:

//Before Spring 21 Release – to access custom metadata type records using SOQL

List<File_Type_Mapping__mdt> soqlFileTypes= [SELECT Id, DeveloperName, MasterLabel, Label, QualifiedApiName FROM File_Type_Mapping__mdt];
System.debug(‘soqlFileTypes ::’+soqlFileTypes);

for(File_Type_Mapping__mdt fileType : soqlFileTypes) {
System.debug(‘fileType DeveloperName ::’+fileType.DeveloperName);
System.debug(‘fileType MasterLabel ::’+fileType.MasterLabel);
System.debug(‘fileType Label ::’+fileType.Label);
}

 

From Spring 21 Release, its possible to access Custom Metadata Type Records Using Static Methods without using SOQL

Use from apex class getAll(), getInstance(recordId), getInstance(qualifiedApiName), and getInstance(developerName) methods to access custom metadata type records values, this methods don’t relay on the SOQL Engine and return as sObject details directly from the method calls.

Release Notes: https://help.salesforce.com/articleView?id=release-notes.rn_forcecom_dev_cmt.htm&type=5&release=230

Example to Access Custom Metadata type records without Using SOQL:

getAll() methods to access custom metadata type records:

//Access to all the records in the custom metadata types and stored in Map
Map<String, File_Type_Mapping__mdt> mapFileTypes = File_Type_Mapping__mdt.getAll();
System.debug(‘fileTypes::’+mapFileTypes);

for(String fileTypeName : mapFileTypes.keySet()){
System.debug(‘fileType Label::’+mapFileTypes.get(fileTypeName).Label);
}

//Convert from Map to List:
list<File_Type_Mapping__mdt> listFileTypes = mapFileTypes.values();
System.debug(‘listFileTypes::’+listFileTypes);
//Iterate listFileTypes variables
for(File_Type_Mapping__mdt fileType : listFileTypes) {
System.debug(‘fileType DeveloperName ::’+fileType.DeveloperName);
System.debug(‘fileType MasterLabel ::’+fileType.MasterLabel);
System.debug(‘fileType Label ::’+fileType.Label);

}

 

getAll() methods to access custom metadata type records in List

List<File_Type_Mapping__mdt> listFileType = File_Type_Mapping__mdt.getall().values();
for(File_Type_Mapping__mdt fileType : listFileType) {
System.debug(‘fileType DeveloperName ::’+fileType.DeveloperName);
System.debug(‘fileType MasterLabel ::’+fileType.MasterLabel);
System.debug(‘fileType Label ::’+fileType.Label);
}

 

Access Custom Metadata Type Single Record value:

List<File_Type_Mapping__mdt> listFileType = File_Type_Mapping__mdt.getall().values();
boolean textField = false;
if (listFileType[0].DeveloperName == ‘bmp’) {
textField = true;
}
System.debug(‘textField::’+textField);

getInstance() methods to access custom metadata type records 

File_Type_Mapping__mdt txtFileTypeDetails = File_Type_Mapping__mdt.getInstance(‘txt’);
System.debug(‘txtFileTypeDetails:: ‘+txtFileTypeDetails);

 

Reference from Salesforce Spring 21 Release Notes:

Custom Metadata Types – Spring 21 – https://help.salesforce.com/articleView?id=release-notes.rn_forcecom_dev_cmt.htm&type=5&release=230

You may also like

Leave a Comment