1

I have a list of cases to be created / updated. There is a Case assignment rule in my org which assigns new cases to a specified user. Now I want to get these cases assigned to the user specified in the user only based on a flag check and otherwise assign to default owners.

I tried the following piece of code but unable to suppress the assignment rule from executing. Any help on this would be appreciated.

List<Case> cases; // Contains new and existing cases 
//This will be set from the UI based on whether the auto assignment of case should happen or not
Boolean useDefaultRule; 

Database.DMLOptions options = new Database.DMLOptions();

options.assignmentRuleHeader.useDefaultRule = useDefaultRule;

List<Case> caseList = new List<Case>();

for(Case case : cases){
   case.setOptions(options);
   caseList.add(case);
}
upsert caseList;

This still triggers auto assignment of rules even if the useDefault flag is set to false. Is there NO way that I can suppress this?

1

Why not go in and edit your assignment rules or create a new one to replace the existing ones? It's simple to do and they're executed in order of precedence. You can put the checkbox as the highest priority (1st) rule that assigns them to the specific user. If the box isn't checked, then the case owner becomes the account owner or whatever default rule you want to configure. That's what point and click is for. There's no need to write code when you can do things using point and click features.

Edit

See the following from the DMLOptions.AssignmentRuleHeaderClass which I think applies to your code:

For the Case sObject, the assignmentRuleID DML option can be set only from the API and is ignored when set from Apex. For example, you can set the assignmentRuleID for an active or inactive rule from the executeanonymous() API call, but not from the Developer Console. This doesn’t apply to leads—the assignmentRuleID DML option can be set for leads from both Apex and the API.

Based on the above, it doesn't appear to me as though you can bypass your defaut assignment rules via Apex.

  • I agree with you on what the point and click functionality is for and also creating or editing assignment rules but my usecase is somewhat like this. There is a scheduled execution of creating or updating cases say everyday from the UI and this function to do the same resides in APEX. – Ashu NCS May 11 '16 at 5:13
  • Now this API always runs properly and when there is any assignment rule, that gets triggered too and cases get assigned appropriately. But suppose I want to provide an option or a flag in the UI to let the user have the say on whether these rules should run or not. How do I handle the flag in the APEX function and get the cases created and assigned accordingly? Is this not possible the way I had mentioned in the question? Should I make use of batch handling or future methods for this? – Ashu NCS May 11 '16 at 5:17
  • See my edit above which I think applies. Are you running the Service Console? If running the Console, see Customizing Case Feed with Code which might possibly help you. – crmprogdev May 11 '16 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.