How to get Custom Metadata in Apex Without SOQL

Published by

on


Custom Metadata details you can directly access without writing a single query in Apex. So we have two scenarios:
1. Get all custom metadata records.
Use getAll()
Returns a map containing custom metadata records for the specific custom metadata type. The map keys are the record DeveloperNames and the map values are the record sObjects.

//Return type list
List<Configurations__mdt> allConfigs = Configurations__mdt.getAll().values(); 
for(Configurations__mdt cofig : allConfigs{
   system.debug('cofig>>'+cofig);
}

//OR, Return type Map
Map<String, Configurations__mdt> allConfigsMap = Configurations__mdt.getAll();
system.debug('get particular configuration>>'+allConfigsMap.get('DeveloperName'));


2. Get specific custom metadata record
Use getInstance(developerName)
Returns a single custom metadata type record sObject for a specified developerName field of the custom metadata type object. Returns null if no record matches the parameter.

Configurations__mdt config = Configurations__mdt.getInstance('DeveloperName');
system.debug('get particular configuration>>'+config);