2

I'm trying to run a unit test to see if the case owner upon insertion matches an expected value. In this case, the expected case owner will be the account owner. However, upon inserting the case, the case owner changes to a queue. The queue in this case is our "default" queue, which is configured in the support settings module. I recently deactivated all of the triggers on the case object to see if the owner was changing after insert, but that does not seem to be the issue.

Test.startTest();

    CreateTicket obj = new CreateTicket();
    Case newCase = obj.createcase();
    System.debug('The owner on the new case is a user' + newcase.ownerid);
    Database.dmloptions dmo = new Database.DMLOPtions();
    dmo.assignmentRuleHeader.useDefaultRule = false;
    newcase.setOptions(dmo);
    insert newCase;
Test.stopTest();

String expectedOwner = 'Expected Owner';
String actualOwner = [SELECT Id, Ownerid FROM Case WHERE Id = :newCase.id LIMIT 1][0].owner.name
System.assertEquals(expectedOwner, actualOwner , 'owners do not match');

Any ideas?

  • 2
    Check your assignment rules – Santanu Boral Mar 30 '17 at 15:09
  • You should really assert on OnwerId, not that it makes much difference here. Perhaps you have a Workflow Rule in play? – Adrian Larson Mar 30 '17 at 15:15
  • 1
    Check your debug logs from running the test. Whatever is going on in should have a hint in the logs. – sfdcfox Mar 30 '17 at 15:15
  • It may not be triggering any assignment rule, so is ending up with the default owner – gorav Mar 30 '17 at 18:38
  • 1
    Something to with OwnerId, I'd imagine. It might be assignment rules, workflow rules, process builder/flows, or apex code. Just anything that seems to be assigning the value. Try searching for the owner's ID in the debug logs, maybe something will show up. – sfdcfox Mar 30 '17 at 21:30
0

So it turns out the test apex execution runs in a different context than the developer console. For this to work through setup, I needed to ensure that the cases are routed through the assignment queues. Previously, the cases would be inserted and the owner would be the user who kicked off the test (me).

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.