List<MyCustomMetadata__mdt> mcm = MyCustomMetadata__mdt.getall().values();
String myFieldValue = mcm[0].MyField__c;
getAll() – Returns a map containing custom metadata records for the specific custom metadata type. The map’s keys are the IDs (as String) of the records and the map’s values are the record sObjects.
getInstance(recordId) – Returns a single custom metadata type record sObject for a specified record ID.
getInstance(developerName) – Returns a single custom metadata type record sObject for a specified developerName field of the custom metadata type object.
getInstance(qualifiedApiName) – Returns a single custom metadata type record sObject for a qualified API name.
Example
This sample returns a single record sObject for the custom metadata type named Games_mdt with developerName specified as FirstRecord.
Games__mdt mc = Games__mdt.getInstance('FirstRecord');
Example with an Apex Class
public class CountryCodeHelper {
public static String getCountryCode(String country) {
Country_Code__mdt countryCode = Country_Code__mdt.getInstance('Canada');
return countryCode.Country_Code__c;
}
}