Save some SOQL queries with the Spring ’21 new Custom Metadata Static Methods

By

2 minutes de lecture

EDIT : Despite the exciting title, I stand corrected: Even when Custom Metadata records are retrieved with SOQL Queries, these are not included in the governor limit as documented here: “custom metadata records can have unlimited SOQL queries”. Thank you to everyone that came to me to let me know.

We all have this kind of project where some part of the application is close to the limit of allowed SOQL queries. Spring ’21 brings us a simple and easy way to remove some of them, let’s take a look.

Getting Custom Metadata records

Using Custom Metadata Types is a great way to store configuration information for your app. It can be configured and deployed easily by an administrator.

Using it in your code requires you to retrieve this information. And today the way to do it is just to query the Custom Metadata records like you would do for any regular sObject:

This is very easy to do, with only one drawback: it consumes one SOQL, which we all know is limited.

Getting Custom Metadata records… starting Spring ‘21

Spring ’21 Release Notes are a beast of 617 pages, full of great updates. But you can’t miss this one: You can now access Custom Metadata Type records using static methods. Example: without using any SOQL query.

It’s now as easy as this:

Notice that you don’t even have to specify the fields you need!

Also, as Custom Metadata Types are now part of the application cache, these methods should be faster than their SOQL counterpart.

Several methods are available to get the Custom Metadata records you need: getAll(), getInstance(recordId), getInstance(qualifiedApiName), and getInstance(developerName. All of them are documented in the Apex Developer Guide.

That’s it! As you can see, there are no drawback on this update, it’s just a better way to get Custom Metadata records. So just stop using SOQL queries for this, and update your existing code to benefit from these new methods!

Would you want to know more about the other updates made to SOQL and Custom Metadata Types in Spring ’21, have a look to Nesrine Ould Braham’s blog post here.

Want to learn more? Read our article Anatomie d’une requête SOQL.

Read more posts

Enforce code standards with PMD

Developers working on a project usually set coding rules to have a standardized codebase. It is an important piece of the code maintainability, and it can be very easy …
March 2023
Advices
Scratch orgs

Uncovering Salesforce Settings: A Step-by-Step Guide for Scratch Orgs

Today, it’s pretty easy to build your Scratch Org definition file when you know what Settings you want to activate, as they are mapped with the same setting names …
February 2023
Advices
Business Analyst

Core qualities of a Business Analyst?

A common definition we are used to hear is that being a Business Analyst means to have a combination of both hard skills and soft skills. What does a …
June 2022
Advices
Image d'illustration d'une employée travaillant sur un ordinateur portable

Process builder and workflow make way to Flows (½)

Overview “If you can do it with a Workflow, then do it with a Process Builder, because everything a Workflow does, a Process Builder does it better”. If you …
March 2022
Advices

Day 22 : Salesforce new “Migrate To Flow tool” in Spring 22

As most of you already know, the workflow rules and process builders are planned to be retired in 2023 (no precise date defined so far). Today, I’m going to …
December 2021
Advices

Day 18 : Fake callout responses for test classes !

Hello everybody ! Today let’s talk about Apex tests classes in Salesforce. Everyone loves a good test class, and Salesforce makes it official by requiring to have a minimum …
December 2021
Advices