I've been struggling for quite some time now to figure out a way to provide the approvers within my company with a list of opportunities that are pending approval and by whom (we have two parallel approval processes in place). The way I'm currently doing it is manually (running a SOQL query in dev console, pushing that to Excel, running an opportunity report, exporting that to Excel, and then merging the files together. Below, gets details from process instance into a visualforce page which is good but doesn't include any fields from opportunity object and also doesn't let me export to Excel.

Would anyone be able to assist me in updating this wrapper class by showing me how to query the opportunity object and including selected fields in VF output? Thanks in advance.

public class getWrapData{

List<wrapperclass> wrapList = new List<wrapperclass>();
public List<wrapperclass> getWrapList() {

    for (ProcessInstance pi: [SELECT Id, Status, TargetObjectId, TargetObject.Name, 
            (SELECT Id, ActorId, Actor.Name, OriginalActorId, OriginalActor.Name, StepStatus, Comments, ProcessNode.Name, CreatedDate FROM StepsAndWorkitems WHERE StepStatus =: 'Pending') 
            FROM ProcessInstance WHERE Status =: 'Pending' ORDER BY TargetObjectId] ) {

               for (ProcessInstanceHistory pih : pi.StepsAndWorkItems) {
                   wrapperClass pendingApprovalWrap = new wrapperClass();
                   pendingApprovalWrap.Status = pih.StepStatus;
                   pendingApprovalWrap.RecordName = String.valueOf(pi.TargetObject.Name);
                   pendingApprovalWrap.RecordId = String.valueOf(pi.TargetObjectId);
                   pendingApprovalWrap.RecordObject = String.valueOf(pi.TargetObjectId.getSObjectType()).split('__')[0];
                   pendingApprovalWrap.AssignedToName = String.valueOf(pih.OriginalActor.Name);
                   pendingApprovalWrap.ApproverName = pih.Actor.Name;
                   pendingApprovalWrap.CreatedDate = String.valueOf(pih.CreatedDate);


               return wrapList;

public class wrapperClass{
    public String Status {get; set;}
    public String RecordName {get; set;}
    public String RecordId {get; set;}
    public String RecordObject {get; set;}
    public String AssignedToName {get; set;}
    public String ApproverName {get; set;}
    public String CreatedDate {get; set;}



TargetObjectId is a polymorphic field so you can't query for the related opp fields directly.

Instead, you have to collect the ids into a set:

Set<Id> oppIds = new Set<Id>();
List<ProcessInstance> piList = [SELECT TargetObjectId, TargetObject.Type, etc etc FROM ProcessInstance WHERE TargetObject.Type = 'Opportunity' AND etc etc];
for (ProcessInstance pi : piList) {

Then query the opportunities into a map using the Id as a key:

Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([SELECT StageName, etc etc FROM Opportunity WHERE Id IN :oppIds]);

Then you loop through your ProcessInstance records again and pull the opp fields from the map using the TargetObjectId:

for (ProcessInstance pi : piList) {
    stage field in wrapper = oppMap.get(pi.TargetObjectId).StageName;
    etc etc etc
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.