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

Quering for ProcessInstanceHistory records in test class gives no results

I am writing a unit test class in which the original class methods submit a record for approval; then they query like this :

[ select id, status, lastmodifieddate,lastmodifiedby.Name, isdeleted,createddate , createdby.Name, targetobjectId , (select Id, IsPending, ProcessInstanceId  , TargetObjectId , StepStatus, OriginalActor.Name , Actor.Name , RemindersSent from StepsAndWorkItems  order by createdDate desc, stepstatus asc)  from ProcessInstance  where TargetObjectId =:testRec.Id ]

the problem is the testRec was succesfully submitted for approval and the ProcessInstance records have Status as 'Pending'  but their children stepsAndWorkItems always return null.This does not happen when I use an original record and try the same in Executing_Anynomous_Window with a real record it gives me StepsAndWorkItems records. Please tell me what to do. Thanks
Hi , 

The line Approval.ProcessResult result = Approval.process(req1); will submit your record to the Approval Process engine that in turn will select a qualifying approval process. The object result can be used to fetch the workItems and then you can simulate approval/reject actions that the Approval Process engine will then execute. Then you can query your database to see if the approval processes as built in the UI are functioning as expected.

See the test class code in the links below,

Shikha Jaiswal TamanShikha Jaiswal Taman
Hi Sreechu,

Did you get any solution for this ? 
I am querying on StepsAndWorkItems  in my trigger which works fine in actual execution, but returns no result in test class execution.

Vipul Sharma 15Vipul Sharma 15
Hi All, 
This is a known issue if you are having a sub query(on ProcessSteps) based on a object and when using the same concept in test class , sub query will be returned as null, so salesforce is very well aware of this , here is the link in case issue affects similar users 

I have also got a workaround in case there is a need for it.... to make the particular method something like this 
@isTest(seeAllData=true) static void submitApproval(){ }
and make all data inside this and once you submit for approval and query on object along with its processSteps , query will have all records.And we can use them as per our needs.