2

I'm writing a unit test to evaluate if a case was successfully assigned to a queue. Unfortunately, the SOQL query I'm using to get the case owner type is returning "null". I've validated that the owner is being set successfully, so I'm not sure why I can't retrieve the owner type. How can I access the case owner type in the SOQL query so that I can evaluate it in the System.assert line (2nd to last line)?

Here's my code:

    @isTest static void insertByPartner() {
    //tests that the partner is added to any cases that they create.

        Case[] cases = TestClassUtils.getCases(1);

        List<Database.SaveResult> results = new List<Database.SaveResult>();

        User partnerTestUser = createPartnerUser();

        Test.startTest();
        System.runAs(partnerTestUser) {
            results = Database.insert(cases, false);
        }
        Test.stopTest();

        Set<ID> caseIdSet = new Set<ID>();
        for(Database.SaveResult sr : results) {
            System.assert(sr.isSuccess());
            caseIdSet.add(sr.getId());
        }

        List<Case> queueCasesList = [SELECT Id, OwnerId, Owner.Type FROM Case WHERE Id IN :caseIdSet AND Owner.Type = 'Queue'];

        for(Case newCase : queueCasesList) {
            System.debug('The case owner is: '+ newCase.OwnerId);
            System.debug('The case owner type is: ' + newCase.Owner.Type);
        }

        System.debug('Cases owned by queue when inserted by a partner user = ' + queueCasesList.size());
        System.assert(queueCasesList.size() == 1);
    }
2

Owner is a polymorphic field and isn't something that should return owner.type in your query. That's not going to resolve for you. Using TYPEOF in a select statement is a new feature of SOQL to support polymorphism in queries that's currently in Developer Preview.

Instead you'd need to query on QueueSobject which could presumably be part of a subquery. Since you've not included all of your code, it's not clear to me whether you're creating the Queue in your code or not. If you are, it would seem there should be other ways of passing the QueueId into your test method.

Alternatively, you could also assert that the OwnerID isn't the UserId of partner who created the case.

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