You need to sign in to do that
Don't have an account?
![Sébastien Chateau Sébastien Chateau](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Case owner in unit test : bypass assignment rules
Hi,
I try to put a user as case owner but when I insert the case I see that the owner is the default case owner configured in the support setting.
To avoid this I tried to bypass assignment rules :
Case c = new Case(); c.ownerId = myUser.id; Database.DMLOptions dmo = new Database.DMLOptions(); dmo.assignmentRuleHeader.useDefaultRule= false; dmo.assignmentRuleHeader.assignmentRuleID = null; c.setOptions(dmo); insert(c);
Nevertheless, the default queue case is always owner.
When we create a case outside unit test, it's created with the good user.
Thanks
And in the code first deactive the assignment rules and then insert the case.
I just had this issue myself where I was running a unit test and noticed my default support user was overwriting the case owner I was setting in apex. I did not observe the same behavior when the record was manipulated through API or manually, so I knew this was specifically a unit test quirk.
I used similar code to you above and it didn't work either. I'm using a before insert trigger and from some of the reading around I did, assignment rules happen after both before and after triggers, so this code was more in mind for @future methods. With that, I then assumed there's no way to get rid of this, so I just had to go in and manipulate my assignment rules.
Here's what I did:
1. I went to Setup | Customize | Cases | Case Assignment Rules
2. I selected the current assignment rule (we only have one active, so this may be more complex for orgs with more than one).
3. I added a new rule entry.
3a. Put it at order #1.
3b. I set it to use a formula - something like this:
- "apex test" is highly unlikely to be used for legit records, so this is a good way to isolate a record meant for testing only.
3c. Instead of assigning to a user or queue, check the "Do Not Reassign Owner" checkbox.
This will make sure your case owner in unit tests are not being overwritten by the case assignements.
Sorry, that code should read this: