Skip to content

Efficient Way to Access Custom Metadata Type in Apex

getAll() to get Custom Metadata Type Records

Now let see how we can get Custom Metadata Type records with getAll() method.

Map<String, Configuration__mdt> configurationMap = Configuration__mdt.getAll();
for(String config : configurationMap.keySet()){
    //access any field as below
    System.debug('----->'+configurationMap.get(config).customField__C);
}

getInstance() to get Custom Metadata Type Record

What about if you don’t need to get all record and need to access only single record? In that case you can use the getInstance() method.

Configuration__mdt mdtd = Configuration__mdt.getInstance('metadatadevelopernamehere');
System.debug('----->'+mdtd);