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
danathandanathan 

Approval History related list visualforce page

I am trying to write a custom Approval History related list visualforce page but not able to group actions for each step.
There is a ProcessInstanceStep object but currently there is no way to identify records in this object that is completed in a specific step (step1, step2, .. etc). Not sure how salesforce standard Approval History related list shows records for each step grouped by step. Can you please suggest a solution ?

 

Looks like salesforce needs to include step number field in ProcessInstanceHistory, ProcessInstanceStep and ProcessInstanceWorkItem objects

 

SurekaSureka

Hi,

 

I had a similar requirement and I was able to display/approve/reject the records

public with sharing class ApproveRejectAllRecs 
{
    public List<ProcessInstance> prosIns{get;set;}
    public boolean rendPgeBlock  {get;set;}
    public String pgMsg{get;set;}
    public string comments{get;set;}
    public List<ProcessInstance> selProsInss{get;set;}
    
    private List<Id> prosInsIds = new List<Id>();    
    private List<ProcessInstanceWorkitem>  prosInsWrkItms = new List<ProcessInstanceWorkitem>();    
    private List<PendingAppRecs>  selVals = new List<PendingAppRecs>();        
    
    public ApproveRejectAllRecs()
    {
        prosIns = new List<ProcessInstance>();
        selProsInss = new List<ProcessInstance>();
        prosIns = [SELECT Id, Status, TargetObject.Id, TargetObject.Name, TargetObject.Type, CreatedDate FROM ProcessInstance where Status='Pending'];
        for(ProcessInstance prIns: prosIns)
        {
            prosInsIds.add(prIns.Id);
        }
        prosInsWrkItms =  [SELECT Id, ProcessInstance.Id, ProcessInstance.Status, Actor.Id, Actor.Name, Actor.Type FROM ProcessInstanceWorkitem where ProcessInstance.Id in : prosInsIds];
    }
    public pagereference appPendingRecs()
    {
        if(!(prosIns.size()>0))
        {
            pgMsg = 'There are no records submitted for approval.';
            rendPgeBlock  = false;
        }
        else
            rendPgeBlock  = true;
        return null;
    }
    
    public void appRej(String appRej)
    {
        for(PendingAppRecs pendApp: getProsInsts()) 
        {
            if(pendApp.selected == true) 
            {
                selProsInss.add(pendApp.proInstce);
            }
        }
        
        for(ProcessInstance pi : selProsInss)
        {
            for(ProcessInstanceWorkitem  pwi  : prosInsWrkItms)
            {
                if(pi.Id == pwi.ProcessInstance.Id)
                {
                    Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
                    req.setComments(Comments);
                    req.setAction(appRej);
                    if(appRej == 'Approve')
                        req.setNextApproverIds(new Id[] {pwi.Actor.id});        
                    List<ProcessInstanceWorkitem> procWrkItms = new List<ProcessInstanceWorkitem>();
                    procWrkItms  = [Select Id from ProcessInstanceWorkitem where ProcessInstance.Id =: pi.Id];                    

                    if((!(procWrkItms.isEmpty()) && procWrkItms[0].Id!=null))
                    {
                        req.setWorkitemId(procWrkItms[0].Id);        
                                            
                        // Submit the request for approval    
                        Approval.ProcessResult result2 =  Approval.process(req);
                    }
                }
            }
        }
    }
    public pagereference approveAll()
    {
        appRej('Approve');
        Pagereference pg = new pagereference('/apex/ApproveRejectAllRecs');
        pg.setredirect(true);
        return pg;
    }

    public pagereference rejectAll()
    {
        appRej('Reject');
        Pagereference pg = new pagereference('/apex/ApproveRejectAllRecs');
        pg.setredirect(true);
        return pg;
    }

    public pagereference cancel()
    {
        return null;
    }
    public pagereference selRecs()
    {
        for(PendingAppRecs pendApp: getProsInsts()) 
        {
            if(pendApp.selected == true) 
            {
                selProsInss.add(pendApp.proInstce);
            }
        }        
        return null;
    }
    public List<PendingAppRecs> proInstsLst{get; set;}
    public List<PendingAppRecs> getProsInsts() 
    {
        if(proInstsLst== null) 
        {
            proInstsLst = new List<PendingAppRecs>();
            for(ProcessInstance p : [SELECT Id, Status, TargetObject.Id, TargetObject.Name, TargetObject.Type, CreatedDate FROM ProcessInstance where Status='Pending']) 
            {
                proInstsLst.add(new PendingAppRecs(p));
            }
        }
        return proInstsLst;
    }
    public class PendingAppRecs
    {
        public ProcessInstance  proInstce{get; set;}
        public Boolean selected {get; set;}
        public PendingAppRecs(ProcessInstance  p) 
        {
        proInstce = p;
        selected = false;
        }
    }
}


<apex:page controller="ApproveRejectAllRecs" action="{!appPendingRecs}">
    <apex:form >
        <apex:pageBlock id="test">        
            <apex:pageBlockButtons >
                <apex:commandButton value="Select Records" action="{!selRecs}" rendered="{!IF(selProsInss.size<=0,true,false)}" reRender="test"/>
                <apex:commandButton value="Approve All" action="{!approveAll}" rendered="{!IF(selProsInss.size>0,true,false)}"/>
                <apex:commandButton value="Reject All" action="{!rejectAll}" rendered="{!IF(selProsInss.size>0,true,false)}"/>                
                <apex:commandButton value="Cancel" action="{!cancel}" rendered="{!IF(selProsInss.size>0,true,false)}"/>
            </apex:pageBlockButtons>
            <apex:pageblockTable value="{!ProsInsts}" var="appRecs" rendered="{!IF(selProsInss.size<=0,true,false)}" width="70%">
                <apex:column >
                    <apex:inputCheckbox value="{!appRecs.selected}"/>
                </apex:column>
                <apex:column headerValue="Related To">            
                    <apex:outputField value="{!appRecs.proInstce.TargetObject.Name}"  />
                </apex:column>
                <apex:column value="{!appRecs.proInstce.TargetObject.Type}"/>
                <apex:column headerValue="Date Submitted for Approval">
                    <apex:outputText value="{0,date,MM'/'dd'/'yyyy}">
                        <apex:param value="{!appRecs.proInstce.CreatedDate}" /> 
                    </apex:outputText>  
                </apex:column>          
            </apex:pageblockTable>
            
            <apex:pageBlockSection columns="1" rendered="{!IF(selProsInss.size>0,true,false)}" id="test1">
                <apex:pageBlockSectionItem >
                <apex:outputLabel value="Comments"/>
                <apex:inputTextArea style="width:600px;height:100px" value="{!comments}"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
                

 

Thanks

 

 

 

danathandanathan

Hi Sureka,

 

This is not solving my issue. I want to display approval history for single submitted record for approval.

Your example is for approving multiple records. It does not display approval actions grouped by each step.

 

Nathan.

nyanginyangi

This is very useful. Will try it out and give you my feedback

RepoRepo
How can i Paginate the Record , standard set controller cannot be used for Process Instance Object.is there a way to paginate the Records form this Controller

Thanks in Advance