Loading

How to Trigger Salesforce Case Assignment Rules from Apex

Publiceringsdatum: Jun 19, 2026
Beskrivning

Overview

Salesforce Case Assignment Rules allow administrators to automatically route Cases to the appropriate user or queue based on configured criteria. In the standard Salesforce UI, a user triggers assignment rules by selecting the "Assign using active assignment rules" checkbox under the Optional section when creating or editing a Case. However, when a Case is inserted via Apex code, assignment rules are not triggered by default because Apex does not present this checkbox option.

Why Assignment Rules Are Not Triggered in Apex

When inserting a Case in Apex without additional configuration, the Case record is successfully inserted but the active Case Assignment Rule is not evaluated. The database insert succeeds, but no user or queue assignment based on the rule criteria occurs. Without DMLOptions, an Apex Case insert creates a new Case record, sets Status to "New", and inserts the record — but the assignment rule does not fire.

Lösning

Resolution: Use Database.DMLOptions to Trigger Assignment Rules

Step 1 — Retrieve the Active Assignment Rule for Case
Use a SOQL query to retrieve the active Assignment Rule for the Case object: query AssignmentRule selecting Id where SobjectType equals "Case" and Active equals true, with LIMIT 1. Store the returned rule's Id.
Step 2 — Create a Database.DMLOptions Instance
Create a new Database.DMLOptions instance. Set dmlOptions.assignmentRuleHeader.assignmentRuleId to the Id retrieved in Step 1. This tells Salesforce which assignment rule to evaluate when the Case is inserted.
Step 3 — Create the Case Record and Attach DML Options
Create a new Case record and set the required fields such as Status. Call newCase.setOptions(dmlOpts) to attach the DMLOptions instance to the Case record before inserting it.
Step 4 — Insert the Case
Insert the Case using the standard insert DML statement. The active Case Assignment Rule now evaluates and routes the Case to the appropriate user or queue based on the configured rule criteria.

Knowledge-artikelnummer

000387623

 
Laddar
Salesforce Help | Article