Custom Metadata & Configuration
What Is Configuration-Driven Apex?
Short Description
Configuration-driven Apex lets you change behavior without changing code—by reading values from configuration instead of hardcoding them.
Simple Explanation
Instead of rewriting code, you flip a switch in settings.
Real-Life Example
Changing a discount rate from 10% to 15% without redeploying code.
Gist (Quick Revision)
Configuration-driven Apex reduces deployments and increases flexibility.
1. Custom Metadata Types (CMDT)
What Are Custom Metadata Types?
Custom Metadata Types store configuration records that:
-
Are deployable
-
Are readable in Apex
-
Do NOT require DML to access
Simple Explanation
CMDT is like a settings table that travels with your code.
Real-Life Example
Different countries have different tax rules—stored as metadata.
Apex Example (Read CMDT)
Discount_Config__mdt config =
Discount_Config__mdt.getInstance('Default');
Decimal discount = config.Discount_Percentage__c;
Why Custom Metadata Is Preferred
-
Deployable between orgs
-
No governor limit for queries
-
Safe for production use
Gist (Quick Revision)
Custom Metadata Types are the best way to store configuration for Apex.
2. Custom Settings
What Are Custom Settings?
Custom Settings store configuration data that can be:
-
Org-wide
-
Profile-specific
-
User-specific
Simple Explanation
Custom Settings are like preferences, different for each user or profile.
Types of Custom Settings
-
List Custom Settings – Similar to a table
-
Hierarchy Custom Settings – User/Profile/Org-level values
Apex Example (Hierarchy Custom Setting)
App_Config__c config = App_Config__c.getInstance(); Decimal maxLimit = config.Max_Limit__c;
When to Use Custom Settings
-
User-specific behavior
-
Profile-based rules
-
Non-deployable config is acceptable
Gist (Quick Revision)
Custom Settings are useful for user- or profile-based configuration.
3. Custom Metadata vs Custom Settings (Easy Comparison)
| Feature | Custom Metadata | Custom Settings |
|---|---|---|
| Deployable | ✅ Yes | ❌ No |
| Governor Limits | No SOQL limits | Uses SOQL |
| Best For | App configuration | User preferences |
| Production Safe | ✅ | ⚠️ Careful |
Easy Rule to Remember
App logic → Custom Metadata
User preference → Custom Settings
4. Configuration-Driven Apex Patterns
Pattern 1: Avoid Hardcoding Values
❌ Bad
Decimal discount = 0.10;
✅ Good
Decimal discount = Discount_Config__mdt.getInstance('Default').Discount_Percentage__c;
Pattern 2: Feature Toggles
Enable or disable features without deployment.
if (Feature_Toggle__mdt.getInstance('NewUI').Is_Enabled__c) {
// new feature logic
}
Pattern 3: Environment-Specific Logic
Different behavior for:
-
Dev
-
Test
-
Production
Gist (Quick Revision)
Configuration-driven Apex makes applications flexible, safer, and faster to change.
