Route Cases with an enum
Think in classes · Intermediate · 15 min · Runs in your Salesforce Developer org · Free
Scenario. A support queue needs a closed set of priority values instead of loosely typed text, and a report that lists every priority.
Model a fixed set of Case priorities, route each one with switch, and loop over them with values().
What you will learn
An enum is a type with a fixed set of named values. switch on handles each one. Every enum has values() (a List, in declared order) and name() (the name as a String).
enum Priority { LOW, HIGH }
String label(Priority p) {
switch on p {
when HIGH { return 'top'; }
when LOW { return 'bottom'; }
}
return 'unknown';
}
Gotcha. Enum values are names like Priority.HIGH, not quoted strings.
The task
Route Case priorities with an enum, then loop over every priority with values().
Examples:
route(CasePriority.HIGH)→High: Immediateroute(CasePriority.MEDIUM)→Medium: Today, andCasePriority.values()has three entries →Priority count: 3- looping over
CasePriority.values()→Route: LOW to Queue, then MEDIUM, then HIGH
Requirements:
- In
route, useswitch onto returnImmediatefor HIGH,Todayfor MEDIUM andQueuefor LOW. - Print
High:andMedium:, each followed by the route for that value. - Print
Priority count:followed by the size ofCasePriority.values(). - Loop over
CasePriority.values()and print oneRoute:line per value withname(), likeRoute: LOW to Queue.
Starter code
enum CasePriority { LOW, MEDIUM, HIGH }
class PriorityRouter {
public String route(CasePriority priority) {
return '';
}
}
PriorityRouter router = new PriorityRouter();
System.debug('High: ' + router.route(CasePriority.HIGH));
When it passes
Each enum value picks one route, and a loop over values() prints all three routes in declared order: LOW, MEDIUM, HIGH.
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.
‹ Overload a Case label method · Recover from division by zero ›