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
Sagar BhatiaSagar Bhatia 

Can anyone explain how ProcessInstance and StepsAndWorkitems works for an approval process?

Hi ALL,

We are trying to access Approval/Rejection comments of the Approval process so that we can make sure the Approver / Rejector add there comments for every step. To do so we found we can access comments using nested query to ProcessInstance along with StepsAndWorkItems , although we are still trying to figure out how exactly StepsAndWorkItems relate with respect to each step in the Approval process. 

When trying to query : SELECT Id, (SELECT Id,ProcessNodeId,ProcessInstanceId,Comments,TargetObjectId FROM StepsAndWorkitems )
FROM ProcessInstance

We could see multiple processNode for one Step, Really confused looking at the data .
User-added image
Best Answer chosen by Sagar Bhatia
Om PrakashOm Prakash
ProcessInstance object Represents an instance of an approval process. which is created every time when a record is submitted for approval.
ProcessInstanceHistory object shows all steps and pending approval requests associated with a ProcessInstance.
ProcessInstanceHistory is created every time when record is submitted for approval, reassigned, approved or rejected.
StepsAndWorkItems is the child relationship name for ProcessInstanceHistory under the ProcessInstance object.

Thats why you have accessed comments using nested query to ProcessInstance along with StepsAndWorkItems.

Hope it clarify your query. Let me know if any confusion.

All Answers

Om PrakashOm Prakash
ProcessInstance object Represents an instance of an approval process. which is created every time when a record is submitted for approval.
ProcessInstanceHistory object shows all steps and pending approval requests associated with a ProcessInstance.
ProcessInstanceHistory is created every time when record is submitted for approval, reassigned, approved or rejected.
StepsAndWorkItems is the child relationship name for ProcessInstanceHistory under the ProcessInstance object.

Thats why you have accessed comments using nested query to ProcessInstance along with StepsAndWorkItems.

Hope it clarify your query. Let me know if any confusion.
This was selected as the best answer
Sagar BhatiaSagar Bhatia
Hi Om,

That was very precise expaination , and well received ;) 
Well there is one thing as mentioned above is the main situation we are stuck in i.e We are looking to block the Approver/ Rejector when comments field is left blank for each step in the approval process.

Is there any way we can validate the Comments fields for Null before saving the Approval/Rejection action ?

Thanks
Om PrakashOm Prakash
We can achieve the same by developing a Visualforce page.
User will approve, reassign or reject from the visuslforce page where we implement the approval logic in Apex code.
In standard way I found bellow blog post. Have a look if it work
http://christopheralunlewis.blogspot.in/2012/10/making-all-approval-comments-mandatory.html
 
Sagar BhatiaSagar Bhatia
This would have been our solution if we didnt had the rejection behaviour as 'Final Behaviour' but its not .
Anyways thanks for the help :) 

Regards,
Sagar