Fetch Metadata Records without using SOQL

Fetch Custom Metadata records in Apex without SOQL

You can save on SOQL query limits when fetching Custom Metadata records.

You can save on SOQL query limits when fetching Custom Metadata records.

Don't do this
List<Custom_App__mdt> customApp = [
    SELECT Id, Class__c
    FROM Custom_App__mdt
    WHERE DeveloperName = 'MyCustomApp'
];

To retrieve a single record by name, simply use the following code:

apex
Custom_App__mdt mc = Custom_App__mdt.getInstance('MyCustomApp');

Check the next page to see how to fetch all Custom Metadata records of a specific type.

To retrieve all records for a specified metadata type, use the getAll() method:

apex
Map<String, Custom_App__mdt> customApps = Custom_App__mdt.getAll();

However, be cautious when using the getAll() method. If your metadata type contains a large amount of data and records, it can consume significant heap space, which may lead to unhandled exceptions. Always check the size of the retrieved data before using it in your code!

Text and code were extracted from the original slide. Plain-text version of the whole catalog