Retrieve Custom Metadata Without Extensive Queries – Spring ’21

Up until the Winter ’21 release, whenever we needed to retrieve all or specific entries of Custom Metadata Types, we had to construct a comprehensive query.

It typically appeared like this:

List<Location__mdt> locationMetadata = [SELECT field1, field2 on Location__mdt];

Now, as part of this release, the process of interacting with Custom Metadata Types has been streamlined for the better.

Two new methods have been introduced with this release:

  • getAll()
  • getInstance() – This method takes a single parameter, which can be either the record ID, developer name, or qualified API name.
//this give us access to all the records in the custom metadata types
List<Location__mdt> loc = Location__mdt.getAll();

//this give us access to the record that's associated with the provided record ID
List<Location__mdt> loc = Location__mdt.getInstance();

//this give us access to the record that's associated with the provided DeveloperName
List<Location__mdt> loc = Location__mdt.getInstance('DeveloperName');

//this give us access to the record that's associated with the provided QualifiedApiName
List<Location__mdt> loc = Location__mdt.getInstance('QualifiedApiName');


That’s quite convenient, isn’t it!

This ensures that the approach to interacting with both Custom Settings and Custom Metadata adheres to the same standards.