Running Case Assignment Rules From Apex

Let’s see here, how to run case assignment rule from trigger.

Case assignment rules allow you to automatically route Cases to the appropriate users or queues. A Case assignment rule consists of multiple rule entries that define the conditions and order for assigning cases. You can create multiple rules (for example, a Standard rule and a Holiday rule), but only one rule can be “active” at a time.

From a standard UI, a user can trigger assignment rules by simply checking the “Assign using active assignment rules” checkbox under the Optional section. The problem arises when your app needs to insert the Case from Apex and wants to trigger assignment rules. Using this script, a Case will be inserted but assignment rules will not be triggered as there is no such field “Assign using active assignment rules” on Case.


A solution is using Database.DMLOptions. The Database.DMLOptions class can provide extra information during a transaction; for example, specifying the truncation behaviour of fields or assignment rule information. For example, the script below is fetching the assignment rules of Case and then creating the DMLOptions for the “Assign using active assignment rules” checkbox.

//Fetching the assignment rules on case
AssignmentRule AR = new AssignmentRule();
AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];

//Creating the DMLOptions for "Assign using active assignment rules" checkbox
Database.DMLOptions dmlOpts = new Database.DMLOptions();

Case newCase = new Case(Status = 'New') ;
//Setting the DMLOption on Case instance
insert newCase ;

Now when the Case is inserted using this script, the assignment rules get triggered.

Source :Knowledge article.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s