Sub topic 6
Routing new cases with assignment rules
This chapter assumes you can navigate Setup, know what a Case record and a queue are, and have read the earlier chapters on case channels and queue ownership. Apex and SOQL knowledge is optional for the code section.
What an assignment rule does
A case assignment rule decides who owns a case when it is created or when it is explicitly re-run. The owner is either a user or a queue. The rule does not change status, priority, or any other field. It only sets OwnerId and, optionally, sends an email notification to the new owner.
Each rule is a container of ordered rule entries. Salesforce evaluates entries from top to bottom and stops at the first entry whose criteria match. That entry's assignee becomes the owner.
Key facts to hold onto:
- You can create many assignment rules, but only one can be active at a time.
- A rule with no matching entry leaves the case with the default case owner. This is set in Support Settings.
- Entries are evaluated by sort order, so order matters more than anything else in the rule.
How a case reaches the rule
Whether the rule runs depends on how the case was created.
| Channel | Does the active rule run? |
|---|---|
| Web-to-Case | Yes, the active rule runs automatically. |
| Email-to-Case and On-Demand Email-to-Case | Yes, the active rule runs automatically. |
| Manual entry in the UI | Only if the user's options or page layout enable it. In Lightning Experience, the New Case form has an "Assign using active assignment rule" checkbox. |
| Apex insert or update | No, unless you set `DMLOptions` in code. |
| API insert | No, unless the assignment rule header is sent. |
The last two rows cause most "the rule is not working" reports. The rule is fine, but the creation path never invoked it.
Building a rule
Go to Setup, search for Case Assignment Rules, and choose New. Give the rule a name and tick Active if it should replace the current active rule. Activating a rule deactivates the previous one, so confirm that nobody depends on it.
Then add rule entries:
- Open the rule and choose New under Rule Entries.
- Enter a sort order. Use gaps such as 10, 20, 30 so you can insert entries later without renumbering.
- Choose the criteria. You can use "Run this rule if the criteria are met" with field conditions, or write a filter logic formula.
- Select the assignee: a user, or a queue.
- Optionally select an email template to notify the new owner.
- Optionally tick "Do Not Reassign Owner" so a case that already has an owner keeps it. This matters when the rule is re-run.
Example entry set
Consider a support team with three queues and a catch-all.
| Sort order | Criteria | Assign to |
|---|---|---|
| 10 | Priority equals High AND Origin equals Phone | Tier2_Support queue |
| 20 | Origin equals Web AND Subject contains "login" | Access_Support queue |
| 30 | Type equals Billing | Billing queue |
| 40 | Origin equals Email | General_Support queue |
A High priority phone case about billing matches entry 10 and never reaches entry 30. If the billing team expected it, the entry order is the cause. Put the most specific entries first and the broadest last.
Criteria and formula choices
Rule entries support two styles of criteria.
- Field criteria compare a field to a value using operators such as equals, contains, and starts with. This is the easiest to maintain.
- Formula criteria use a formula that returns true or false. Use this for logic that field criteria cannot express, such as
ISPICKVAL(Priority, "High") && Account.Type = "Customer".
Be careful with blank values. A condition like "Type not equal to Billing" matches cases where Type is empty. If you do not want that, add a second condition that Type is not blank.
Queues as assignees
Assigning to a queue only works if Case is listed as a supported object on that queue. If it is not, the queue will not appear in the assignee lookup of the rule entry. Check this first when a queue seems to be missing.
When an entry assigns to a queue, Owner.Type on the case is Queue. The case then waits until a queue member takes ownership, or until a later rule or escalation moves it. This is the state the monitoring query in a later chapter looks for.
Notifications from the rule
If you select an email template on an entry, Salesforce emails the new owner. For a queue, the email goes to the queue email address if one is set, and to members depending on the queue's "Send Email to Members" setting. If neither is configured, nobody is notified, even though the assignment succeeded.
Running the rule from Apex
Cases inserted from Apex do not run assignment rules unless you set DMLOptions. This snippet asks Salesforce to apply the active rule on insert.
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);
insert c;You can also target a specific rule with dmo.assignmentRuleHeader.assignmentRuleId, but that ties the code to a record Id that differs between orgs. Using the active rule is easier to deploy.
If you want to skip the rules and set the owner yourself, assign a queue Id directly. A queue is stored as a Group record of type Queue.
Group tier2 = [
SELECT Id
FROM Group
WHERE Type = 'Queue' AND DeveloperName = 'Tier2_Support'
LIMIT 1
];
Case c = new Case(
Subject = 'Data export fails',
Origin = 'Email',
OwnerId = tier2.Id
);
insert c;Query by DeveloperName, not Name, because the label can be edited and differs by language.
To re-run the rule on existing cases, set the options on the records and call update. Remember that "Do Not Reassign Owner" on an entry will keep the current owner in that case.
Common errors
- Rule is inactive. Only an active rule runs. Check the Active column on the list of rules.
- Wrong entry order. A broad entry placed early captures cases meant for a later one.
- Apex or API created the case. No header or
DMLOptionswas supplied, so the rule never ran. - Queue does not support Case. The queue cannot be picked as an assignee.
- Criteria use a field the creator cannot populate. A web form that does not collect the field always evaluates it as blank.
- Owner changed unexpectedly. Re-running the rule on updates reassigns owners unless "Do Not Reassign Owner" is ticked.
- Default owner is not what you expect. A case that matches no entry goes to the default case owner in Support Settings, which is easy to forget.
Testing a rule
Create test cases through each channel you support, not only the UI. For each one, check the owner, the owner type, and the entry you expected to match. A short query helps with queue-owned cases:
SELECT Id, CaseNumber, Subject, Status
FROM Case
WHERE Owner.Type = 'Queue' AND IsClosed = false
ORDER BY CreatedDate ASCA case that landed with the default owner instead of a queue points to no matching entry or a rule that did not run.
Exercise
A Web-to-Case submission with Priority High and Type Billing lands in the GeneralSupport queue, although you expected the Billing queue. Your entries are: 10 Origin = Web, assign to GeneralSupport; 20 Type = Billing, assign to Billing. What is wrong, and how do you fix it?
Show answer
Entries are evaluated by sort order and the first match wins. The case matches entry 10 because its Origin is Web, so entry 20 is never evaluated. Move the more specific Billing entry above the broad Origin entry (for example, give Billing sort order 10 and the Web entry sort order 20), or add a condition to the Web entry that excludes Billing cases.
Exercise
A scheduled Apex job inserts cases from an external feed, and none of them are routed to queues, although the active assignment rule works for Email-to-Case. Why, and what do you add?
Show answer
Apex inserts do not run assignment rules by default. Create a Database.DMLOptions, set assignmentRuleHeader.useDefaultRule to true, call setOptions on each Case, and then insert. Alternatively, set OwnerId to a queue's Group Id directly.