Boosting Salesforce Performance with Custom Metadata Types: A Complete Guide

Published in Developer
May 04, 2025
2 min read
Boosting Salesforce Performance with Custom Metadata Types: A Complete Guide

Boosting Salesforce Performance with Custom Metadata Types: A Complete Guide

Salesforce offers a rich set of features for automation, scalability, and customization. Yet many Salesforce professionals overlook one of its most powerful tools: Custom Metadata Types (CMDTs). This guide explores how CMDTs can dramatically improve performance, enable cleaner configurations, and reduce technical debt—while still giving you room to scale.

Why Custom Metadata Types Matter

Hardcoded values, switch-case logic, or environment-specific configurations in Apex code are common culprits for performance issues and maintenance nightmares. Custom Metadata Types solve these problems elegantly by allowing metadata to be:

  • Deployed from sandbox to production
  • Referenced directly in code
  • Managed without updates to Apex classes

CMDTs turn environment-specific configuration into scalable, declarative metadata.

What Are Custom Metadata Types?

CMDTs are similar to custom objects but intended solely for configuration data. Unlike custom settings, CMDTs are deployable, versionable, and accessible in Apex, flows, and formulas.

Key Features:

  • Deployable via change sets or metadata API
  • Queryable with SOQL
  • Version-controlled in source-driven development
  • Compatible with validation rules and workflow

Real-World Use Cases for CMDTs

1. Feature Flags and Toggle Logic

You can control feature visibility using CMDTs to enable/disable components or automation rules without code changes.

public class FeatureToggle {
public static Boolean isFeatureEnabled(String featureName) {
Feature__mdt setting = [SELECT IsEnabled__c FROM Feature__mdt WHERE DeveloperName = :featureName LIMIT 1];
return setting.IsEnabled__c;
}
}

2. SOQL Query Definitions

Store reusable query filters or criteria as CMDT records. This improves query readability and promotes reuse.

3. API Endpoint Management

CMDTs are ideal for storing different API endpoints per environment (dev, test, prod). No more hardcoded strings!

Creating a Custom Metadata Type

  1. Go to SetupCustom Metadata Types
  2. Click New Custom Metadata Type
  3. Define your label, object name, and optional description
  4. Add custom fields as needed

Example: API Configuration Type

Fields:

  • Environment__c (Picklist: Dev, Test, Prod)
  • Endpoint__c (Text)
  • IsActive__c (Checkbox)

Referencing CMDTs in Apex

CMDTs are queryable using standard SOQL syntax.

List<API_Config__mdt> configs = [SELECT Environment__c, Endpoint__c FROM API_Config__mdt WHERE IsActive__c = true];
for (API_Config__mdt cfg : configs) {
System.debug('Endpoint for ' + cfg.Environment__c + ': ' + cfg.Endpoint__c);
}

CMDTs vs Custom Settings: Key Differences

FeatureCMDTsCustom Settings
Deployable❌ (limited)
Version-controlled
Usable in Apex/Flows
UI Management
SOQL Support
Metadata TypeYesNo

Best Practices for Using CMDTs

1. Use Naming Conventions

Always follow a naming convention (e.g., FeatureToggle__mdt, ApiSettings__mdt) to keep things clean and searchable.

2. Avoid Over-Engineering

Don’t overuse CMDTs for minor constants. Use them only when logic is likely to vary across environments or evolve.

3. Cache Your CMDT Queries

Avoid repetitive SOQL calls by caching CMDT values using static variables or custom wrapper classes.

4. Leverage Validation Rules

Apply validation rules to ensure metadata integrity—just like you would with regular objects.

Advanced Use Case: Declarative Control of Automation Rules

Imagine you have an Apex trigger that should only fire if the organization is in a specific state. Use a CMDT to declaratively manage this logic.

if (AutomationControl__mdt.getInstance('AccountTrigger').IsEnabled__c) {
// run trigger logic
}

CMDTs in Flows and Formulas

CMDTs can be accessed in Flow Builder using the “Get Records” element. This allows admins to dynamically control logic without touching code.

Example:

  • Use CMDTs to manage picklist value mappings
  • Trigger record flows based on CMDT-driven criteria

Conclusion

Custom Metadata Types offer a powerful way to move logic out of Apex and into configurable metadata, improving scalability and maintainability. They empower both developers and admins to build cleaner, more dynamic Salesforce orgs.

Ready to boost your org’s agility? Start replacing hardcoded logic with CMDTs today.

Got questions or want to see a real-world implementation? Drop a comment or explore our step-by-step examples!


Share