11

I'm trying to use Custom metadata type to replace a List type Custom settings. I have to access the Custom metadata type information in multipe apex classes and triggers.

When I use custom settings, I can access them from apex using,

List<customSettingsObject__c> cs = customSettingsObject__c.getall().values();

I dont have to use a SOQL Query for custom settings.

But to access Custom metadata type in apex, I have to use a soql,

List<custMetadataType__mdt> mcs = [select id,MasterLabel from custMetadataType__mdt];

I don't want to query since I have to use it in multiple classes and triggers. Any other way to access Custom metadata type in apex without SOQL?

18

No, you must access them with a SOQL statement. However, SOQL queries against custom metadata do not increase the SOQL queries governor limit counter nor the SOQL query rows governor limit counter, so you can query them as frequently as you like.

0
11

Just An Update: Salesforce is coming up with new methods to retrieve custom metadata type records in a similar way custom setting records are retrieved. Yes, Without SOQL query. and It's happening in the Spring 21' release.

Below is a glimpse of functionality from Spring 21' pre-release org.

enter image description here

3
  • 4
    This is a welcome addition to the Custom Metadata feature that will help align it with Custom Settings.
    – sfdcfox
    Jan 25 '21 at 14:46
  • 2
    In case someone needs a link to the official salesforce docs for this feature. developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/…
    – Bahman.A
    Jun 11 '21 at 18:18
  • 2
    An important note in the docs for using the getInstance() methods: "Only the first 255 characters of any field in a custom metadata type record are returned. Therefore, fields such as long text fields can be truncated. If you want all the field data from a custom metadata type record, use a SOQL query."
    – Bahman.A
    Jun 11 '21 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.