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
Keith987Keith987 

How to find the approvers for outstanding approval process instances?

The code below is my first attempt at solving this problem based on inspecting the relevant objects via the developer console. But I have not found any documentation that confirms that this logic is appropriate. Please comment if you now of such documentation or have a cleaner or proven solution.

 

Thanks,

Keith

Force201

 

// Return payment id to set of approver ids
private Map<Id, Set<Id>> readApprovers(Set<Id> paymentIds) {
    
    // Get the latest ProcessInstance for each payment
    Map<Id, ProcessInstance> m = new Map<Id, ProcessInstance>();
    for (ProcessInstance pi : [
            select Id, TargetObjectId, (select ActorId from Steps where StepStatus not in ('Approved', 'Rejected'))
            from ProcessInstance
            where TargetObjectId in :paymentIds
            and IsDeleted != true
            and Status not in ('Approved', 'Rejected')
            order by CreatedDate desc
            ]) {
        if (m.get(pi.TargetObjectId) == null) {
            m.put(pi.TargetObjectId, pi);
        }      
    }
    
    // Ensure always at least an empty set not null
    Map<Id, Set<Id>> results = new Map<Id, Set<Id>>();
    for (Id paymentId : paymentIds) {
        results.put(paymentId, new Set<Id>());
    }
    
    // Find any actors who have not processed their step
    for (ProcessInstance pi : m.values()) {
        for (ProcessInstanceStep pis : pi.Steps) {
            if (pis.ActorId != null) {
                results.get(pi.TargetObjectId).add(pis.ActorId);
            }
        }
    }
    
    return results;
}

 

colemabcolemab

I have a great write up on how to automatically email the current approver here. for outstanding approval requests only of course.

JimBenJimBen
@colmab Is there an updated link for your article? The linked URL won't load fully.