2

This is exectuted in apex test. I want to insert a record, but skip Case assignment rules:

    ...
    newCase.OwnerId = someUser.Id;
    Database.DMLOptions options = new Database.DMLOptions();
    options.AssignmentRuleHeader.UseDefaultRule = false;
    newCase.setOptions(options);

    insert newCase;
    // same result as this -> Database.insert(newCase, options);

But after inserting owner is group:

    Case tempCase = [SELECT Id, OwnerId FROM Case];
    System.debug('Case owner type: ' + Id.valueOf(tempCase.OwnerId).getSobjectType());

Prints this:

|DEBUG|Case owner type: Group

Issue is definitely in assignment rules, because if I disable it in setup, code works as expected.

4
  • Are you sure there isn't a Workflow Rule or Process Builder at play?
    – Adrian Larson
    Commented Oct 18, 2016 at 15:11
  • 1
    I believe that the default behavior for Apex is to not run assignment rules. (this was something I ran into trying to get cases created from async apex failures to send me an email about the new case.) What happens if you just do a normal insert without setting any DMLOptions? Pretty sure this has to be either Workflow or Process as Adrian suggests.
    – Derek F
    Commented Oct 18, 2016 at 15:12
  • 1
    @AdrianLarson when I disable assignment rules in setup, it works as expcted Commented Oct 18, 2016 at 15:20
  • @DerekF no process or workflow for this. Same error with no DML options Commented Oct 18, 2016 at 15:20

1 Answer 1

1

If I run test from developer console, it fails. (Case owner is Group)

If I run test from Illuminated Cloud, it fails. (Case owner is Group)

If I run test from Welkin Suite, it succeeds. (Case owner is User)

If I run test from Setup -> Develop -> Apex Test Execution, it succeeds. (Case owner is User)

3
  • @AdrianLarson I have no idea why, but other developers say they have seen issues like this before - when tests in dev console fail, but are successful from SF setup Commented Oct 19, 2016 at 10:19
  • @DerekF this is what happens Commented Oct 19, 2016 at 10:19
  • I've seen this problem as well. Try running the test in Async mode in the developer console (checkbox "Always run Asynchronously" under the Test menu dropdown). Illuminated Cloud added support for forcing all tests to run in Async as well.
    – dcinzona
    Commented Apr 18, 2017 at 21:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .