Build Configurable Business Rules (Custom Metadata)
By the end of this lesson, you'll be able to:
- Query Case_Routing_Rule__mdt and build an in-memory lookup structure
- Match a Case's Priority and Type against the configured rules
- Explain why the metadata is queried once per transaction, not once per Case
Prerequisites: "Enforce Field-Level Security"
Querying the rules once
List<Case_Routing_Rule__mdt> rules = [
SELECT Priority__c, Case_Type__c, Queue_Name__c
FROM Case_Routing_Rule__mdt
];
List<Case_Routing_Rule__mdt> rules = [— custom metadata is queried with regular SOQL (Module 29).SELECT Priority__c, Case_Type__c, Queue_Name__c— the fields needed.FROM Case_Routing_Rule__mdt— the object.];— closes the query — critically, this runs once per transaction, at the top of the handler method, following the same "collect, then act" instinct from Module 25.
Building a fast lookup key
Map<String, String> queueByPriorityAndType = new Map<String, String>();
for (Case_Routing_Rule__mdt rule : rules) {
String key = rule.Priority__c + '|' + rule.Case_Type__c;
queueByPriorityAndType.put(key, rule.Queue_Name__c);
}
Map<String, String> queueByPriorityAndType = new Map<String, String>();— the lookup, keyed by a composite string.- (blank line)
for (Case_Routing_Rule__mdt rule : rules) {— loops over the rules, in memory.String key = rule.Priority__c + '|' + rule.Case_Type__c;— combiningPriority__candCase_Type__cinto a single composite key (Module 16's Map lesson, applied with a small trick: concatenating two values into one key).queueByPriorityAndType.put(key, rule.Queue_Name__c);— stores the queue under that key.}— closes the loop.
This lets every subsequent lookup be a single, instant Map.get() instead of looping through the rules list again for every Case.
Matching a Case against the rules
public void beforeInsert(List<Case> newCases) {
// ... field-access check from the previous lesson ...
Map<String, String> queueByPriorityAndType = buildRoutingLookup();
for (Case c : newCases) {
String key = c.Priority + '|' + c.Type;
if (queueByPriorityAndType.containsKey(key)) {
c.Assigned_Queue__c = queueByPriorityAndType.get(key);
}
}
}
private Map<String, String> buildRoutingLookup() {
List<Case_Routing_Rule__mdt> rules = [SELECT Priority__c, Case_Type__c, Queue_Name__c FROM Case_Routing_Rule__mdt];
Map<String, String> lookup = new Map<String, String>();
for (Case_Routing_Rule__mdt rule : rules) {
lookup.put(rule.Priority__c + '|' + rule.Case_Type__c, rule.Queue_Name__c);
}
return lookup;
}
public void beforeInsert(List<Case> newCases) {— the entry point.// ... field-access check from the previous lesson ...— unchanged from before.- (blank line)
Map<String, String> queueByPriorityAndType = buildRoutingLookup();— the lookup is built once.- (blank line)
for (Case c : newCases) {— loops over the new Cases.String key = c.Priority + '|' + c.Type;— builds the same kind of composite key.if (queueByPriorityAndType.containsKey(key)) {— checks the lookup.c.Assigned_Queue__c = queueByPriorityAndType.get(key);— assigns the matching queue, oneMap.get()each, entirely in memory.}— closes theif.}— closes the loop.}— closes the method.- (blank line)
private Map<String, String> buildRoutingLookup() {— the helper.List<Case_Routing_Rule__mdt> rules = [SELECT Priority__c, Case_Type__c, Queue_Name__c FROM Case_Routing_Rule__mdt];— the one query.Map<String, String> lookup = new Map<String, String>();— the lookup being built.for (Case_Routing_Rule__mdt rule : rules) {— loops over the rules.lookup.put(rule.Priority__c + '|' + rule.Case_Type__c, rule.Queue_Name__c);— stores each one.}— closes the loop.return lookup;— hands back the completed lookup.}— closes the helper.
However many Cases there are in newCases, this is exactly Module 25's bulkification discipline, now applied to configuration data instead of database records.
Exercise
Given Priority = 'High' and Type = 'Problem' on a Case, build the composite lookup key the same way buildRoutingLookup does.
Show hint
c.Priority + '|' + c.Type
Build Configurable Business Rules (Custom Metadata) Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
This lesson reads the configurable routing rules from custom metadata and builds a fast lookup structure, so the actual per-Case matching (next lesson) never needs to search the rules list repeatedly.