We have code that will submit an Opportunity for approval when saved at a certain stage. When I do this manually in the UI I see the Opportunity is submitted for approval. I can query the ProcessInstance, ProcessInstanceSteps and ProcessInstanceWorkitems tables and see all the items created using the "Force.com Explorer (beta)".
I have unit test code that creates an Opportunity, updates a custom field to Stage 2 which should submit it for approval. I can query and see everything other than the ProcessInstanceWorkitems created, without these I can't reject or approve the Opportunity to verify the other actions fire correctly. Should I be able to process an Approval in a unit test? For a change I'm actually hoping I've coded something wrong.
//Code to create an Opportunity o comes here, then 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.id]; System.assert(processInstanceSteps.size() > 0,'No approval processes steps found when saving at Stage 2');
Everything is fine until this point. I now want to create a ProcessWorkItemRequest and process the Workitems to reject/approve the Opportunity and assert it's done what I expect. However the statement below (and all the commented out versions I've run) indicate there are no ProcessInstanceWorkitems at all.
//List<ProcessInstanceWorkitem> processInstanceWorkitems = [SELECT ActorId, CreatedById, Id FROM ProcessInstanceWorkitem WHERE ProcessInstanceId = :processInstances.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]; List<ProcessInstanceWorkitem> processInstanceWorkitems = [SELECT ActorId, CreatedById, Id FROM ProcessInstanceWorkitem]; System.assert(processInstanceWorkitems.size() > 0,'No approval processes Workitems found when saving at Stage 2');
Any offers to what I'm missing or what I've done wrong? My list of processInstanceWorkitems is always zero size.