12

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?

2 Answers 2

20

Edit: The rows themselves now count against the 50k SOQL row limit, although they do not count against the 100 SOQL query limit. To access custom metadata records without consuming SOQL rows is to use the getAll and getInstance methods, outlined in the documentation. If you need the full value from an Long Text Area field, however, you may need to use SOQL statements, as LTA fields are truncated to the first 255 characters when using getAll or getInstance.


Original:

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.

5
  • The SOQL query rows governor limit applies actually for Custom Metadata Type, check response here: salesforce.stackexchange.com/a/333271/36830 And also now is possible to retrieve records without SOQL, as @Akshay Poddar has also responded here. May 19, 2022 at 11:56
  • 1
    @EnrikHysko This answer was correct at the time of its publication. I do have 12k+ answers at the time of this comment, so it's not possible for me to monitor all the answers against all future changes. Note that the new getAll and getInstance methods do still have limitations against LTA fields in an MDT, so there are times when you may need to query. Thanks for calling this out, though, I'll amend this answer.
    – sfdcfox
    May 19, 2022 at 12:30
  • 1
    @EnrikHysko And seeing my previous comment on the other answer, it's likely I meant to edit this last year but forgot about it with everything going on in the world. I always appreciate feedback on my answers when they become outdated.
    – sfdcfox
    May 19, 2022 at 12:35
  • you're welcome. I totally understand that and thanks for updating the answer! May 20, 2022 at 9:52
  • Important note regarding the LongTextArea fields: if your SOQL query includes the LongTextArea field as one of the queried fields, then this CMT query still counts against the # Of SOQL Queries governor limit. Doc: help.salesforce.com/s/… Have just confirmed this in an anonymous Apex. Jan 20 at 12:11
13

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, 2021 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, 2021 at 18:18
  • 3
    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, 2021 at 18:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .