Running Case Assignment Rules From Salesforce Apex

Running Case Assignment Rules From Salesforce Apex

Hi All,

We all know that the Case Assignment Rule decides how cases are assigned to users or put into queues. We can create multiple assignment rules, but only one rule can be "active" at a time.

We can trigger assignment rules by simply checking the "Assign using active assignment rules" checkbox under the Optional section of the Case creation page from Standard UI. Or We can set it default true under the "Layout Properties"of the case page layout.

case assignment

But do you know!!! When we are inserting the Case via Apex, the assignment rule will not be triggered as there is no such field "Assign using active assignment rules" to be checked in the Apex.

DON'T WORRY. Here in this blog, you will know how to run Assignment Rule through Salesforce Apex.

//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();
dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;
Case newCase = new Case(Status = 'New') ;

//Setting the DMLOption on Case instance
newCase.setOptions(dmlOpts);
insert newCase ;

The above piece of code is fetching the active assignment rule of Case and then creating the DMLOptions for the "Assign using active assignment rules" checkbox. Actually Database.DMLOptions class provides advanced information.

So now when the Case is inserted through Apex with the Database.DMLOptions, the assignment rule gets invoked easily.

Happy Salesforce!

Popular Salesforce Blogs