Access Custom Metadata Without A Full Blown Query - Spring '21

Access Custom Metadata Without A Full Blown Query - Spring '21

Until Winter 21 release, whenever we had to fetch all or certain entries of a Custom Metadata Types, we had to write a full-blown query.

And it's going to look something like this!

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

Now it's all simplified, as a part of this release the way we can interact with Custom Metadata Types is changed a bit, for good.

There are 2 new methods which are available to us as a part of the release.

  • getAll()
  • getInstance() - This method accepts one parameter and it can be either 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 pretty much handy is it!

This makes sure the way we interact with Custom Settings and Custom Metadata follow the same standards.

Subscribe to Salesforce Casts

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe