3

I'm trying to query for Approval Items which are outstanding for the running user to approve, filtering these approvals by the object type they are related to.

I've attempted to use the following SOQL, which works fine as an administrator:

     SELECT Id, ProcessInstance.TargetObjectId
       FROM ProcessInstanceWorkitem
USING SCOPE Mine_And_My_Groups
      WHERE ProcessInstance.Status = 'Pending'
             AND ProcessInstance.ProcessDefinition.TableEnumOrId IN ('Account', 'Contact')

Running as a "Standard User" profile I've encountered an error message:

System.UnexpectedException: Illegal filter on ProcessInstanceWorkitem

I've narrowed to down to the filter over ProcessInstance.ProcessDefinition.TableEnumOrId which is causing the issue. I can remove this filter, and subsequently filter by calling getSObjectType() against the ProcessInstance.TargetObjectId value, but would prefer to keep this efficient by filtering upfront.

Can anyone explain why my filter is illegal?

Test class which reproduces the issue:

@IsTest
static void testAsAdmin() {

    User u = getTestUser([SELECT Id FROM Profile WHERE Name = 'System Administrator'][0].Id);

    System.runAs(u) {
        //SUCCEEDS
        List<ProcessInstanceWorkitem> workitems = [
                SELECT Id, ProcessInstance.TargetObjectId
                FROM ProcessInstanceWorkitem
                USING SCOPE Mine_And_My_Groups
                WHERE ProcessInstance.Status = 'Pending'
                AND ProcessInstance.ProcessDefinition.TableEnumOrId IN ('Account', 'Contact')
        ];
    }
}

@IsTest
static void testAsStandardUser() {

    User u = getTestUser([SELECT Id FROM Profile WHERE Name = 'Standard User'][0].Id);

    System.runAs(u) {

        //FAILS
        List<ProcessInstanceWorkitem> workitems = [
                SELECT Id, ProcessInstance.TargetObjectId
                FROM ProcessInstanceWorkitem
                USING SCOPE Mine_And_My_Groups
                WHERE ProcessInstance.Status = 'Pending'
                AND ProcessInstance.ProcessDefinition.TableEnumOrId IN ('Account', 'Contact')
        ];
    }
}

static User getTestUser(Id profileId) {
    return new User(
            Alias = 'test',
            Email = 'exampletestuser@example.com',
            EmailEncodingKey = 'UTF-8',
            FirstName = 'Testing',
            LastName = 'Testing',
            LanguageLocaleKey = 'en_US',
            TimeZoneSidKey = 'America/Los_Angeles',
            LocaleSidKey = 'en_US',
            UserName = 'exampletestuser@example.com',
            ProfileId = profileId
    );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.