ApexSensei

Drills › Think in classes

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: Immediate
  • route(CasePriority.MEDIUM) → Medium: Today, and CasePriority.values() has three entries → Priority count: 3
  • looping over CasePriority.values() → Route: LOW to Queue, then MEDIUM, then HIGH

Requirements:

  1. In route, use switch on to return Immediate for HIGH, Today for MEDIUM and Queue for LOW.
  2. Print High: and Medium:, each followed by the route for that value.
  3. Print Priority count: followed by the size of CasePriority.values().
  4. Loop over CasePriority.values() and print one Route: line per value with name(), like Route: 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.

Try this drill free

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 ›