Hardcoded Config Values Instead of Custom Metadata Types
“Hardcoded thresholds, API endpoints, and business rules in Apex. Every change required a deployment.”
What Happened
I built an approval routing system for Mobilization Funding where deals over $500K went to the VP and deals over $1M went to the CEO. I hardcoded those thresholds in Apex. Two months later, the thresholds changed. Then they changed again. Each time required a code change, test class update, deployment, and UAT. The client started asking why a 'simple number change' took a full sprint. They were right to ask.
The Wrong Way
public class ApprovalRouter {
public static String getApprover(Decimal amount) {
if (amount > 1000000) {
return 'CEO'; // Hardcoded
} else if (amount > 500000) {
return 'VP_Sales'; // Hardcoded
}
return 'Manager'; // Hardcoded
}
// Threshold changes = code change = deployment
// "Why does changing a number take 2 weeks?"
}The Right Way
// Custom Metadata Type: Approval_Threshold__mdt
// Fields: Min_Amount__c, Max_Amount__c, Approver_Role__c
//
// Records (deployable AND admin-editable):
// High: Min=1000000, Max=null, Approver=CEO
// Medium: Min=500000, Max=1000000, Approver=VP_Sales
// Low: Min=0, Max=500000, Approver=Manager
public class ApprovalRouter {
public static String getApprover(Decimal amount) {
for (Approval_Threshold__mdt threshold :
Approval_Threshold__mdt.getAll().values()) {
Decimal min = threshold.Min_Amount__c;
Decimal max = threshold.Max_Amount__c;
if (amount >= min
&& (max == null || amount < max)) {
return threshold.Approver_Role__c;
}
}
return 'Manager';
}
// Threshold changes = admin edit. No deployment.
// Cached. No SOQL. Free.
}The Lesson
If a value might change, put it in Custom Metadata Types. They're deployable, admin-editable, cached, and don't count against SOQL limits. Hardcoded values in Apex are a maintenance nightmare.
Enjoyed this? Get more like it.
Glen's Musings — AI, investing, and building things. Occasional. Free.
More Architecture Mistakes
5 Process Builders, 3 Flows, 2 Triggers on One Object
Stacked automations from three different consultants over two years. Nobody knew what fired when.
Read moreAnnoyingBuilding Custom Objects When Standard Objects Already Exist
Built a custom Project_Tracker__c object. Turns out Salesforce has a perfectly good Task and Event system.
Read morePainfulThe God Object: One Custom Object to Rule Them All
Created one custom object with 200 fields to handle every use case. It handled none of them well.
Read more