Sub topic 10
Testing and Troubleshooting Routing and Escalation
This chapter assumes you can create queues, build assignment rules with ordered rule entries, define business hours, and build escalation rules. It also assumes you can read basic SOQL. Here you will learn how to prove that routing and escalation work, and how to find the cause when they don't.
Why routing bugs are hard to see
Assignment and escalation rules fail quietly. A case that matches no rule entry still saves. A rule that was never activated raises no error. The case simply lands with the wrong owner, or with the default owner, and nobody notices until a customer complains.
Testing therefore means checking outcomes: who owns the case, and whether the escalation flag changed. You do not wait for an error message.
The two rule types also run at different times:
| Rule type | When it runs | What it changes |
|---|---|---|
| Assignment rule | When a case is created, or when a user chooses to reassign with the active rule | Owner, and optionally an email notification |
| Escalation rule | In the background, when a case meets an age condition | Owner, the Escalated flag, and notifications |
Testing assignment rules
Test in a sandbox or in a Developer Edition org first. Use a short checklist for each rule entry.
- Confirm that exactly one assignment rule is active. Salesforce allows only one active assignment rule per object.
- Write one test case per rule entry. Fill in only the fields that entry uses, so a pass cannot come from a different entry.
- Create each case through the channel you care about.
- Check the owner on the saved record.
- Add a case that matches nothing and confirm it reaches the fallback you designed.
Creating test cases through each channel
The creation path decides whether the rule runs at all.
- Manual entry in the console: the case form has an option to assign using the active assignment rule. If it is cleared, the rule does not run and the case goes to the creating user.
- Web-to-Case and Email-to-Case: these apply the active assignment rule. Cases created through them are owned by whoever the rule selects. If no entry matches, they fall to the default case owner set in the channel settings.
- Apex: the rule does not run unless you ask for it with
DMLOptions. - Data Loader and the API: the rule runs only when you send the assignment rule header.
This Apex test creates a case with the rule applied, then checks the result. Note that Apex tests cannot see the data in the org. They run against rules that exist in the org, but you must create your own queue and users.
@IsTest
private class CaseRoutingTest {
@IsTest
static void highPriorityWebCaseUsesAssignmentRule() {
Case c = new Case(
Subject = 'Cannot log in to the portal',
Origin = 'Web',
Priority = 'High'
);
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
c.setOptions(dmo);
Test.startTest();
insert c;
Test.stopTest();
Case saved = [SELECT OwnerId, Owner.Type FROM Case WHERE Id = :c.Id];
System.assertNotEquals(null, saved.OwnerId, 'Case must have an owner');
System.assertEquals(
'Queue',
saved.Owner.Type,
'High priority web cases should land in a queue'
);
}
}The assertion depends on the org's active rule. A test like this is useful in a sandbox deployment pipeline. It fails if someone changes the rule order or deactivates the rule.
Why a case goes to the wrong place
| Symptom | Likely cause | Fix |
|---|---|---|
| Case is owned by the creating user | The rule was not applied on this creation path | Select the assignment option, set `DMLOptions`, or send the header |
| Case goes to the default owner | No rule entry matched | Correct the criteria, or add a last entry with no criteria |
| Case goes to the wrong queue | An earlier entry matches too broadly | Move the specific entry above the broad one |
| Rule seems to be ignored | Rule is inactive, or a different rule is active | Activate the intended rule |
| Save fails or assignment is skipped | Target user is inactive, or the queue does not support Case | Pick an active user, and add Case to the queue's supported objects |
| Assignment to a user fails | User lacks access to the object or record | Check profile or permission set access |
Entry order is the most common cause. The rule evaluates entries top to bottom and stops at the first match. A broad entry such as Priority = High placed above Priority = High AND Origin = Email means the second entry never runs.
Testing escalation rules
Escalation is harder to test because it depends on time. Salesforce evaluates escalation rules on a schedule, not in the instant a case ages past a limit. Expect small delays, so do not test with a window of one minute and expect a precise result.
Use this approach:
- Set a short age limit in a sandbox, such as 60 minutes, for a test rule entry.
- Create a case that matches the entry. Make sure it matches no other entry, since the first match wins.
- Confirm the case meets the "not closed" condition. Closed cases never escalate.
- Wait for the age limit plus a margin, then check the case.
- Verify that the owner changed, the Escalated flag is set, and the notification email arrived.
What "age" means
The age limit counts from the case's creation, or from the last modification, depending on the option on the rule entry. When the setting is "Based on last modification", every edit resets the clock. A case that agents touch often will never reach the limit. This surprises many admins.
The age limit also counts only business hours when a business hours record is chosen on the rule entry. If the entry uses the default business hours, a case created at 17:30 on Friday may take until the following Monday to reach a four-hour limit.
Why a case did not escalate
| Symptom | Likely cause | Fix |
|---|---|---|
| Nothing happens at all | The escalation rule is inactive | Activate it. Only one escalation rule is active at a time |
| Case is old but not escalated | Business hours stop the clock | Check the hours and holidays on the rule entry |
| Case was edited and the clock reset | Age is based on last modification | Switch to creation-based age if that fits your policy |
| Case is closed | Closed cases are skipped | Check the Status and `IsClosed` |
| Case matches a different entry | An earlier entry matched | Reorder entries, and tighten criteria |
| Escalated, but no email | Notification template or recipient problem | Check the template, and that the notification user has an email address |
| Case escalated more than once | Several actions with different ages | Review each action's age limit |
Checking results with queries
Queries are faster than clicking through records. These two queries from the monitoring chapter double as test checks.
SELECT Id, CaseNumber, Subject, Priority, Owner.Name, CreatedDate
FROM Case
WHERE IsEscalated = true AND IsClosed = false
ORDER BY CreatedDate ASCIf a test case is missing from this result after the expected age, escalation did not fire. Look at the table above.
SELECT Id, CaseNumber, Subject, Status
FROM Case
WHERE Owner.Type = 'Queue' AND IsClosed = false
ORDER BY CreatedDate ASCCases owned by a queue are waiting for an agent. If your test case should have reached a queue and is not here, check which owner it has instead.
You can also inspect the queue itself. A queue is stored as a Group record, and the object types it accepts are stored in QueueSobject.
SELECT Queue.Name, SobjectType
FROM QueueSobject
WHERE SobjectType = 'Case'If your queue is missing here, it cannot own cases. This is one of the most common reasons a rule entry fails to assign to a queue.
Using history to see what happened
Turn on field history tracking for Owner and Status on Case. After a test, the history related list shows who changed the owner and when. An escalation shows the change made by the automated process. A manual change shows a user name. This tells you whether the rule or a person moved the case.
Debug logs also help. Set a trace flag on the user who creates the case, then reproduce the scenario. Logs show Apex triggers, flows, and workflow actions that run on the same case. Another automation may be overwriting the owner after the rule sets it. Flows or triggers that set OwnerId after assignment are a frequent hidden cause, and a rule that "works" in isolation can look broken in a full org.
A repeatable test plan
- Test in a sandbox with realistic queues and users.
- Keep a table of rule entries, one row per entry, with an example case and the expected owner.
- Run each row after any change to rules, queues, or business hours.
- Include a negative case that matches nothing.
- Re-test escalation with business hours at the edges: just before closing time, over a weekend, and on a listed holiday.
- Record the results, and automate the assignment checks in Apex where you can.
Exercise
A High priority email case should go to the Tier2Support queue, but it keeps landing in a general queue. The rule has an entry for Priority = High listed above an entry for Priority = High AND Origin = Email, which points to Tier2Support. What is wrong, and how do you fix it?
Show answer
Assignment rule entries are evaluated top to bottom and the first match wins. The broad High priority entry matches the case first, so the more specific Email entry never runs. Move the specific entry above the broad one, then create a test case with Priority High and Origin Email and confirm the owner is Tier2_Support.
Exercise
An escalation rule entry is set to escalate cases after four hours. A case created at 17:30 on Friday is still not escalated on Saturday morning, although the rule is active. Give two possible explanations and one way to check each.
Show answer
First, the entry uses business hours that exclude weekends or evenings, so the clock has not run for four hours. Check the business hours record on the rule entry. Second, the age may be based on last modification and an agent edited the case, which reset the clock. Check the rule entry's age setting and the case history. Closed status is a third possibility.