function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AgarAgar 

No ProcessInstanceWorkitem created for Approval Process test

I'm testing a trigger on our Opportunities which kick off an Approval Process when the Opportunity's Process_Stage__c field changes. If I create an Opportunity and move it between stages through the UI I can see the ProcessInstance, ProcessInstanceStep and ProcessInstanceWorkitem are all created. I'm using Force.com Explorer Beta and have verified this. So far so good.

 

In my test class I'm creating an Opportunity and inserting it, then updating the Process_Stage__c. After the update I'm seeing the ProcessInstance and ProcessInstanceStep are being created but there are no ProcessInstanceWorkitems at all in the database. I want to test rejecting and approving the Approval but without the ProcessInstanceWorkitem I can't do this (as far as I understand the Approval Process).

 

Below is my code, there are a few lines where I've tried various ways to retrieve the ProcessInstanceWorkitems, finally resulting in [SELECT ActorId, CreatedById, Id FROM ProcessInstanceWorkitem]; because up until this point I thought my SOQL was wrong, now I don't think the ProcessInstanceWorkitems are being created for some reason. Is there something fundamental I'm missing? Should this be possible to do from a unit test?

 

// Yadda yadda yadda Create Opportunity (o) here
		insert(o); 
		
        // Make sure this isn't already queued up for approval before saving as a Stage 2
		List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :o.id];
		System.assertEquals(processInstances.size(),0,'Approval Process already triggered when saving at Stage 1');

		o.Process_Stage__c = TestConstants.STAGE_2; 
		update(o);

        // Assert an approval process has been initiated
		processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :o.id];
		System.assertEquals(processInstances.size(),1, 'Only 1 approval processes should be created when saving at Stage 2, found '+processInstances.size());
		
		List<ProcessInstanceStep> processInstanceSteps = [SELECT ActorId, OriginalActorId, StepStatus 
			FROM ProcessInstanceStep 
			WHERE ProcessInstanceId = :processInstances[0].id];
        System.assert(processInstanceSteps.size() > 0,'No approval processes steps found when saving at Stage 2');

        List<ProcessInstanceWorkitem> processInstanceWorkitems = [SELECT ActorId, CreatedById, Id FROM ProcessInstanceWorkitem]; // WHERE ProcessInstanceId = :processInstances[0].id];
        //List<ProcessInstanceWorkitem> processInstanceWorkitems = [Select p.Id from ProcessInstanceWorkitem p where p.ProcessInstance.TargetObjectId = :o.id];
        //List<ProcessInstance> processInstancesWithWorkitems = [SELECT Id, (SELECT Id, ActorId, ProcessInstanceId FROM Workitems) FROM ProcessInstance WHERE TargetObjectId = :o.id];
		System.assert(processInstanceWorkitems.size() > 0,'No approval processes Workitems found when saving at Stage 2');
 
Mo.SMo.S
Hi, I had same issue, try insert his line before your test class, "@isTest(SeeAllData=true)". I used this and it fixed my issue, hopefully it fixes your issue.
AgarAgar
Thanks for the response Mo. Unfortunately I'm looking to write proper unit tests, this means fundamentally not relying on production data in my Org which is what that flag does.
isTest + SeeAllData = Contradiction

I will try investigating this further though as I don't see why visibility of data should affect the creation of the ProcessInstanceWorkItems, this is an interesting point and may hold some clues to the answer.

Melissa Mueller 9Melissa Mueller 9
I'm having the same problem with process instance workitems not being created. I had already added (SeeAllData=true) because of another data access issue and I am still not getting any work items. If you solved this, I would love to know how!
DalDal
@Melissa

did you find a solution for the problem? I got the same error !!!