Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (6) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) Force.com tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Running Case Assignment Rules From Apex

Running Case Assignment Rules From Apex

Post by Ankit Arora (forceguru)  (2012-01-23)

Status: Unverified
Level: intermediate

Problem

Running Case assignment rules while inserting a case from Apex.

Description

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.

//Instance of case
Case newCase = new Case(Status = 'New') ;
//Inserting a Case
insert newCase ;

Solution

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();
dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;

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

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

References

Share

Recipe Activity - Please Log in to write a comment

BTW, the answer to my question is "Yes".  I was able to confirm that you can definitely cause the Assignment Rules to be triggered on an update.

by jodell07032012  (2013-10-01)

Nice article, Ankit!

Question: do you know whether I could invoke these assignment rules when a case is updated (rather than created)?  I have a use case where if a field in the case is changed, it should re-assign the case according to the assignment rules.

by jodell07032012  (2013-09-25)

X

Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on Force.com. When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.