Dec 8, 2011

Case Assignment Rules From Apex - Database.DMLOptions

This is really very important for users who use native cases with assignment rules.


I have assignment rule defined on Case which says if the "Status" of case is "New" then assign that case to "System Admin" (User in my organization)




Now when I insert a case manually with "Assignment using active assignment rules" checkbox checked then assignment rule works fine, and "System Admin" is assigned as the Case Owner.


Now I want to create the case from my apex code and also want assignment rules to be triggered. But the problem is we are not able to access the "Assignment using active assignment rules" checkbox in apex. Now am stuck because there is a lot of code already written for case insertion, so what is the solution for this?


This code :
Case newCase = new Case(Status = 'New') ;
insert newCase ;


inserts the case but assignment rules are not triggered so case owner in my scenario remains "Ankit Arora" and expected was "System Admin"






Solution is using Database.DMLOptions. Now before inserting the case I use Database.DMLOptions and with my case insertion assignment rules are also triggered, this is all what I want.


When I use this code :
//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') ;
newCase.setOptions(dmlOpts);
insert newCase ;


It inserts the case and my assignment rule which is in active state get fired and Case Owner becomes "System Admin"



6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. It's more reliable to use the default option:

    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule= true;
    newCase.setOptions(dmo);

    Cheers
    Detlef

    ReplyDelete
  3. Do you have a test class for this?

    ReplyDelete
  4. Nice Blog! Experience the Best Assignment Writing Services at Assignment Help Sydney Australia

    ReplyDelete