Enable Assignment rules from Apex

Enable Assignment rules from Apex

Usecase

Running Case/Lead assignment rules while inserting a case from Apex.
Note: In this example we deal with Case

For Triggers

From a standard UI, a user can trigger assignment rules by simply checking the "Assign using active assignment rules" checkbox under the Optional section.

Solution

//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', Subject="Dummy Subject") ;
//Setting the DMLOption on Case instance
newCase.setOptions(dmlOpts);
insert newCase ;

Now Case Assignment rules will be triggered

Happy coding! Please comment for any help

Subscribe to Phanindra Mangipudi

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe