• JimBen
  • NEWBIE
  • 0 Points
  • Member since 2018
  • Software Developer
  • Ethos Solution

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

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;
}