+ Start a Discussion
SreechuSreechu 

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
Ashish_SFDCAshish_SFDC
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 force.com UI are functioning as expected.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_process_example.htm

See the test class code in the links below, 

http://salesforce.stackexchange.com/questions/17981/processinstanceworkitems-not-created-when-running-an-approval-process-unit-test

http://stackoverflow.com/questions/9594709/how-do-i-test-a-trigger-with-an-approval-process

http://blog.jeffdouglas.com/2010/01/04/automating-salesforce-approval-processes-with-apex-triggers/


Regards,
Ashish
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.


regards,
Shikha 
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
https://success.salesforce.com/issues_view?id=a1p30000000SWMFAA4 

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.
Rgds
Vipul