Sub topic 12
Running Assignment Rules and Setting Queue Owners from Apex
This chapter assumes you know what a case assignment rule and a queue are, and that you can read basic Apex and SOQL. It covers two ways to route a case from code: asking Salesforce to run the active assignment rule, and setting a queue as the owner yourself.
Why Apex does not route cases by default
Assignment rules run automatically when a case is created through Web-to-Case, Email-to-Case, or the UI with "Assign using active assignment rules" checked. A case inserted from Apex does not run them unless you ask. Without that request, the case is owned by the running user, or by the default case owner if a trigger or flow sets one.
You have two options:
- Run the active assignment rule with
Database.DMLOptions. - Set
OwnerIddirectly to a user or queue Id.
Option 1: Run the assignment rule
Database.DMLOptions carries settings that change how a DML statement behaves. Its assignmentRuleHeader property controls assignment rules. Setting useDefaultRule to true applies the active case assignment rule.
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;Call setOptions before the DML statement. The options apply to that record for that call only. If you use Database.insert(c, dmo) instead, pass the same options object as the second argument.
If you need a specific rule rather than the active one, set assignmentRuleHeader.assignmentRuleId to that rule's Id. Do not set both useDefaultRule and assignmentRuleId.
Option 2: Set a queue as the owner
A queue is stored as a Group record with Type = 'Queue'. Its Id is valid in OwnerId on a case. Query by DeveloperName, not by the label, because the developer name does not change when someone renames the queue in Setup.
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;The queue must list Case as a supported object. If it does not, the insert fails with an error about an invalid owner.
Choosing between the two
| Need | Approach |
|---|---|
| Admins maintain routing without code changes | Assignment rule through `DMLOptions` |
| One known destination, such as an integration that always sends to Tier 2 | Set `OwnerId` to the queue Id |
| Routing depends on data only code can compute | Set `OwnerId` |
Common errors
- Case stays with the running user. The rule was not requested, or no rule entry matched and no default owner is configured.
- Queue query returns no rows. The
DeveloperNameis wrong, or you left outType = 'Queue'. Without that filter you may also match a public group. - Invalid owner on insert. The queue does not support the Case object.
- Query in a loop. Querying the queue for each case in a trigger consumes SOQL limits. Query once before the loop and reuse the Id.
Verifying the result
After running your code, check which cases landed in a queue:
SELECT Id, CaseNumber, Subject, Status
FROM Case
WHERE Owner.Type = 'Queue' AND IsClosed = false
ORDER BY CreatedDate ASCExercise
You insert a Case from an Apex trigger and find it is owned by the user who triggered the insert, even though an active assignment rule matches the case. What is missing, and what code fixes it?
Show answer
The code never requested the assignment rule. Create a Database.DMLOptions object, set dmo.assignmentRuleHeader.useDefaultRule = true, call c.setOptions(dmo) before the insert (or pass dmo to Database.insert), and then insert the case.
Exercise
Why query a queue by DeveloperName with Type = 'Queue' instead of by Name?
Show answer
The developer name stays the same when the queue is renamed, so the code keeps working. The Type filter ensures you match a queue and not a public group that shares the name.