Custom Metadata Types in Salesforce provide a powerful way to store configuration and application metadata. They are highly customizable and can be accessed in Apex code, making them essential for building scalable and maintainable applications. In this article, we will explore how to access Custom Metadata Type records using static methods in Apex and discuss how to query custom metadata using GraphQL in Lightning Web Components (LWC).
Accessing Custom Metadata Type Records Using Static Methods
Custom Metadata Types can be accessed in Apex code using static methods. This approach provides efficient access to metadata records without the need for SOQL queries, which helps avoid hitting governor limits.
What Are Custom Metadata Types?
Custom Metadata Types are similar to Custom Settings but offer more flexibility and are deployable across environments. They allow you to define custom metadata that can be packaged and deployed, and their records can be accessed directly in Apex code.
Using Static Methods to Access Custom Metadata
Salesforce provides a way to access Custom Metadata records through static methods generated for each Custom Metadata Type. These methods are available in the format [CustomMetadataTypeName]__mdt.[RecordName]
.
Example: Suppose you have a Custom Metadata Type named My_Settings__mdt
with a record named Default_Settings
. You can access this record in Apex using the following syntax:
My_Settings__mdt defaultSettings = My_Settings__mdt.getInstance('Default_Settings');
Here, getInstance()
is a static method that retrieves the Custom Metadata record with the specified name.
Benefits of Using Static Methods
Using static methods to access Custom Metadata Types offers several advantages:
- No SOQL Limits: Accessing records via static methods does not consume SOQL queries, helping to stay within governor limits.
- Compile-Time Checking: Since the methods are known at compile time, you get syntax checking and code completion in your IDE.
- Performance: Static method access is generally faster than querying via SOQL.
Accessing All Records of a Custom Metadata Type
To access all records of a Custom Metadata Type, you can use the getAll()
static method:
Map<String, My_Settings__mdt> settingsMap = My_Settings__mdt.getAll();
This returns a map of all records, keyed by their developer names.
Example: Using Custom Metadata in Apex
Let’s consider a scenario where you have a Custom Metadata Type named Discount_Rate__mdt
that stores discount rates based on customer tiers.
Step 1: Define the Custom Metadata Type and Records
Discount_Rate__mdt
with fields:Customer_Tier__c
(Text)Discount_Percentage__c
(Number)
- Records:
Gold
: 20%Silver
: 10%Bronze
: 5%
Step 2: Access Custom Metadata Records in Apex
public static Decimal getDiscountRate(String customerTier) {
Discount_Rate__mdt discountRecord = Discount_Rate__mdt.getInstance(customerTier);
if (discountRecord != null) {
return discountRecord.Discount_Percentage__c;
} else {
return 0;
}
}
In this example, the method getDiscountRate
retrieves the discount percentage based on the customer tier using the static getInstance()
method.