3

No matter what to do, the case is always assigned by the assignment rule!!

You can try the scenario and code which are attached in the post.

But, I have tried to execute the code in the developer console, and it is ok.

The whole test scenario is as below:

[Case assignment rule] <== only 1 rule for testing

Criteria:

ISPICKVAL(Type, 'Other')

Assign to:

Case default queue

[Testing class]

@isTest(SeeAllData=true)
private class TestCases {
  static testMethod void unitTest01() {
    Case caCheck;
    Case c = new Case();
    c.Type = 'Other';
    insert c;
    caCheck = [ SELECT Id,Owner.Name FROM Case WHERE Id =: c.Id ];
    System.assertEquals(caCheck.Owner.Name, Userinfo.getName()); // Owner is changed to 'Case default queue'
  }
  static testMethod void unitTest02() {
    Case caCheck;
    Case c = new Case();
    c.Type = 'Other';
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule= false;
    c.setOptions(dmo);
    insert c;
    caCheck = [ SELECT Id,Owner.Name FROM Case WHERE Id =: c.Id ];
    System.assertEquals(caCheck.Owner.Name, Userinfo.getName()); // Owner is changed to 'Case default queue' 
  }
}

[Testing result]

2 errors:

1.

System.AssertException: Assertion Failed: Expected: Case default queue, Actual: Cray Kao
Class.TestCases.unitTest01: line 9, column 1

2.

System.AssertException: Assertion Failed: Expected: Case default queue, Actual: Cray Kao
Class.TestCases.unitTest02: line 20, column 1
  • 1
    Update: Try to run the test in the developer console, it will always fire case assignment rule. But, when doing the test in the classical UI, it will pass. Both of test are with the same code. It looks like a bug. – Cray Kao Apr 19 '16 at 7:37
  • I've seen the same behavior in the developer console. The solutions below are good, but they don't address the situation when you want to test whether or not the assignment rules are firing under different conditions (for example, if you have a trigger that invokes assignment rules for some cases but not others). This seems like a bug with developer console. – Charles Naughton Dec 12 '16 at 23:24
5

I have tried this and found that I can get it to both work and to fail. Let me explain how.

I took the basic principle as the OP;

  • A single Case Assignment Rule, assigning to a queue with the criteria ISPICKVAL(Type, 'Other')
  • A simple test class to test assignment rules both on and off. I modified the test case to be a little clearer but the principle is basically the same...

    @isTest(SeeAllData=true)
    private class CaseAssignmentRuleTest{
    
        private static testMethod void testAssigmentRuleOn(){
    
            // Build my new case
            Case c = new Case(Type = 'Other');
    
            // Set options to ignore assignment
            Database.DMLOptions dmo = new Database.DMLOptions();
            dmo.assignmentRuleHeader.useDefaultRule = true;
            c.setOptions(dmo);
    
            // Run the test
            Test.startTest();
    
            insert c;
    
            Test.stopTest();
    
            // Get the case that we just inserted
            Case caCheck = [SELECT Id, OwnerId FROM Case WHERE Id = :c.Id];
    
            // The owner should *NOT* be me
            System.assertNotEquals(Userinfo.getUserId(), caCheck.OwnerId, 'Should be the queue not the user');
    
        }
    
        private static testMethod void testAssignmentRuleOff(){
    
            // Build my new case
            Case c = new Case(Type = 'Other');
    
            // Set options to ignore assignment
            Database.DMLOptions dmo = new Database.DMLOptions();
            dmo.assignmentRuleHeader.useDefaultRule = false;
            c.setOptions(dmo);
    
            // Run the test
            Test.startTest();
    
            insert c;
    
            Test.stopTest();
    
            // Get the case that we just inserted
            Case caCheck = [SELECT Id, OwnerId FROM Case WHERE Id = :c.Id];
    
            // The owner should be me
            System.assertEquals(Userinfo.getUserId(), caCheck.OwnerId, 'Should be me as the owner NOT the queue');
        }    
    
    }
    
  • I am working with version 37 of the platform, however v29 gives the same results.

Running this test class from the Developer Console, I get a pass and a fail the testAssigmentRuleOn test passes whilst the testAssigmentRuleOff test fails.

testAssigmentRuleOn pass, testAssigmentRuleOff fail

This shows that the assignment rule runs regardless of the DML options that passed. This is inline with the experience of the OP.

So how did I get it to work? I simply reran the tests from the dev console however this time I forced them to run asynchronously.

testAssigmentRuleOn pass, testAssigmentRuleOff pass

TL;DR

  • Running tests synchronously you cannot override assignment rules
  • Running tests asynchronously you can override assignment rules
0

I tried the same code with some modifications and found that it works as expected.

Case 1:

dmo.assignmentRuleHeader.useDefaultRule= false

Result

The case was assigned to user who created the Case record i.e. me.

Case 2:

dmo.assignmentRuleHeader.useDefaultRule= true

Result

The case was assigned using active assignment rule to a queue I have defined.


If I delete the assignment rule and use useDefaultRule as true, the case is assigned to Default Case Owner(I am on API v29).

This is working as expected in my organization as defined in the documentation.

Please check if you have a trigger which is assigning the case to the default queue or a workflow along with your Default Case Owner.


Edit

I tried using seeAllData as true at I also observed the same behaviour as OP.

  • I changes the version of the test class to v29 and the test result is still the same (The assignment rule is executed). It looks more like the bug as now. And, thanks you for helping the test. – Cray Kao Mar 11 '16 at 11:38
  • The org we use is a developer org and the instance is na15. So why is the other possible for me to check?........... so curious to know the root cause. – Cray Kao Mar 11 '16 at 11:39
  • By the way, do you use "SeeAllData=true" in the test class? – Cray Kao Mar 11 '16 at 11:42

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.