Get Current Approver using Salesforce Apex

Get Current Approver using Salesforce Apex

ProcessInstance object/entity contains the current pending Approval Step. TargetObjectId can be used to get the Approval Step for the record. Using the ProcessInstance, we can query the ProcessInstanceWorkitem object/entity to get the current or next approver on which the record is waiting to complete the Approval Process in Salesforce.

Sample Apex Code:

String recordId = 'a00Ho00000NVCj2IAH';

List < ProcessInstance > listPIs = [ 
    SELECT Id
    FROM ProcessInstance
    WHERE TargetObjectId =: recordId
    AND Status = 'Pending'
    LIMIT 1
];

if ( listPIs.size() > 0 ) {
    
    ProcessInstance objPI = listPIs.get( 0 );
    
    if ( objPI != null ) {
        
        ProcessInstanceWorkitem objPIWI = [ 
            SELECT Id, Actor.Name
            FROM ProcessInstanceWorkitem
            WHERE ProcessInstanceId =: objPI.Id
            LIMIT 1
        ];
        
        System.debug(
            'Current Approver: ' +
            objPIWI.Actor.Name
        );
        
    }
    
}

Apex trigger will not be invoked as a result of initiating or completing an approval process.

Help Article:
https://help.salesforce.com/s/articleView?id=000383878&type=1

Idea for this feature:
https://ideas.salesforce.com/s/idea/a0B8W00000GdeyQUAR/allow-an-approval-process-to-trigger-a-flow

We can also make use of Salesforce Lightning Web Component to display the Approver details.

Sample Lightning Web Component:

Apex Class:

public with sharing class ApprovalProcessController {

    @AuraEnabled( cacheable=true )
    public static string getCurrentApprover( String recordId ) {

        String strCurrentApprover;
        List < ProcessInstance > listPIs = [ 
            SELECT Id
            FROM ProcessInstance
            WHERE TargetObjectId =: recordId
            AND Status = 'Pending'
            LIMIT 1
        ];
        
        if ( listPIs.size() > 0 ) {
            
            ProcessInstance objPI = listPIs.get( 0 );
            
            if ( objPI != null ) {
                
                ProcessInstanceWorkitem objPIWI = [ 
                    SELECT Id, Actor.Name
                    FROM ProcessInstanceWorkitem
                    WHERE ProcessInstanceId =: objPI.Id
                    LIMIT 1
                ];
                
                System.debug(
                    'Current Approver: ' +
                    objPIWI.Actor.Name
                );
                strCurrentApprover = objPIWI.Actor.Name;
                
            }
            
        }
        
        return strCurrentApprover;
        
    }

}

HTML:

<template>
    <lightning-card>
        <h1 slot="title">
            Current Approver Information
        </h1>
        <div class="slds-p-horizontal_small">
            {currentApprover}
        </div>
    </lightning-card>
</template>

JavaScript:

import { LightningElement, wire, api } from 'lwc';
import getCurrentApprover from '@salesforce/apex/ApprovalProcessController.getCurrentApprover';

export default class CurrentApprover extends LightningElement {

    @api recordId;
    currentApprover;
    
    @wire( getCurrentApprover, { recordId: '$recordId' } )
    currentApprover( { error, data } ) {
        if ( data ) {
            
            console.log(
                'Current Approver:',
                data
            );
            this.currentApprover = data;

        } else if ( error ) { 

            console.log(
                'Error occurred:',
                error
            );
            this.currentApprover = undefined;

        }
    }

}

js-meta.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>62.0</apiVersion>
    <isExposed>true</isExposed> 
    <targets> 
        <target>lightning__RecordPage</target> 
    </targets> 
</LightningComponentBundle>

Leave a Reply