Oh yes! You read that right.
Custom metadata has been around for a few years and given the ease of being able to deploy them unlike custom settings, we always had to query them in our apex as below.
List<Configuration__mdt> configurationMetadata = [SELECT ConfigField1__c, ConfigField2__c FROM Configuration__mdt LIMIT 100];
It is now as easy as getting getting all of our custom settings and looks like below.
//access all the records in the custom metadata type
List<Configuration__mdt> config = Configuration__mdt.getAll();
//access the record associated with the provided record ID
List<Configuration__mdt> config = Configuration__mdt.getInstance();
//access the record associated with the provided DeveloperName
List<Configuration__mdt> config = Configuration__mdt.getInstance('DeveloperName');
//access to the record associated with the provided QualifiedApiName
List<Configuration__mdt> config = Configuration__mdt.getInstance('QualifiedApiName');
The very two important methods available to us are:
- getAll() — Fetches all the records for a custom metadata type
- getInstance() — This method accepts parameters like record Id, DeveloperName or QualifiedApiName.