Friday, August 6, 2021

SOQL query on Custom-Metadata counts against Governor Limits ?

We all have been using custom metadata types in various instances as it is so advantageous over List custom-settings.  Be it the availability of more field types or page-layouts or List Views.  Gotta admit that Custom-Medatadata is obviously a goto place over list custom setting.  But many developers tend to write a SOQL for fetching a record from custom-metadata with a conception that SOQL on CustomMetadata won't count against governor limits.  

Yes, SOQL on Custom Metadata won't count against Governor Limits if and only if you are not querying for a field of type "Long Text Area".  But if your query includes a field of type "Long Text Area", then it is counted against SOQL Governor Limits.   

Let us understand it better with the below example..

There is a custom metadata with API name as "cmd__mdt" and is having two Custom fields 

  1. Status (Status__c) of type "Picklist".
  2. Description(Description__c) of type "Long Text Area"
      

Let us write a SOQL to fetch the "Status__c" field from Custom Metadata.  This query won't be counted against Governor Limits and so we are good here.

Now when we write similar SOQL to fetch the "Description__c" from metdata, this query counts against the governor limits and this is where we have to look at.


Refer to the below link for few such other limitations
https://help.salesforce.com/articleView?id=sf.custommetadatatypes_limits.htm&type=5

The main motivation for us to use Custom Metadata and query over it is that it wont count against SOQL Governor Limits, but with this limitation it sounds more like an impediment now.  

Fortunately from Spring'21, we are able to access Custom Metadata by using static methods but not by using SOQL.  Look at the below snippet


Just like custom setting, we have some static methods to access the data in custom-metadata and as there is no SOQL written here, so no worry on SOQL Governor Limits.  You can use these methods in your Apex Class by upgrading the API Version to latest one, else it might throw an error, as this was released recently and not available in the older API versions.  

It is suggested to replace SOQL on Custom Metadata with static methods available and have no worry on the SOQL Governor Limits.   Happy Coding..

Methods available for Custom metadata are present in the below link.  

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_custom_metadata_types.htm

Hope you find this interesting and feedback is much appreciated

...Bazingaa...

6 comments:

  1. Thanks for this well written, clear explanation. Very helpful!

    ReplyDelete
  2. It also counts if queried in Flow vs Apex. That's not actually documented by Salesforce, which it should be.

    ReplyDelete
    Replies
    1. It is mentioned in the SF documentation. Refer to the link
      https://help.salesforce.com/articleView?id=sf.custommetadatatypes_limits.htm&type=5

      Delete
  3. Interesting fact! Thanks for sharing.

    ReplyDelete