1

I have this Approval Scenario where I want to have this feature where :

If one user presses the "submits for approval" button on the Account record, the approval process will go to 5 users to ask for their approval. Only if AT LEAST 2 of the 5 approvers approve then the Account will be Finally Approved.

Approval Steps

As in the picture above, I want a radio button to have an option to 'approve based on the first n response' (n meaning any number).

Is there a simple way around this using OOTB way, apex, apex trigger or any other ways?

** UPDATE **

Here is my attempt of using Apex Trigger to count the number of "Status = Approved" from the Approval History related list of Account (My SOQL doesnt seem to work):

trigger ApprovalHistoryCountTrigger on ProcessInstance (after insert, after update, after delete, after undelete) {
Map<Id,List<ProcessInstance>> originalFileIds = new Map<Id,List<ProcessInstance>>();
Set<id> fileIds = new Set<id>();

if (Trigger.new <> null){
    for(ProcessInstance c:Trigger.new){
        if(c.TargetObjectId != null)
            fileIds.add(c.TargetObjectId);
    }

} else if(Trigger.old != null){
    for(ProcessInstance c:Trigger.old){
        if(c.TargetObjectId <> null)      
            fileIds.add(Trigger.oldMap.get(c.id).TargetObjectId);
    }
}

if (fileIds.size() > 0){
    try{
        List<ProcessInstance> cdlList = new List<ProcessInstance>();
        Map<id,Account> testmap = new Map<id,Account>(
            [select id, Count_AA_Approvals__c from Account where id IN: fileIds]);
        cdlList = [SELECT id, TargetObjectId 
                   FROM ProcessInstance 
                   WHERE TargetObjectId IN:fileIds  
                     AND ID in (SELECT ProcessInstanceId FROM ProcessInstanceSteps WHERE StepStatus = "Approved")
                     AND ID in (SELECT ProcessInstanceId FROM ProcessInstanceNode WHERE DeveloperName = "AA_to_Approve")  ];

        for (ProcessInstance cdl : cdlList){
            List<ProcessInstance> cdlList2 = new List<ProcessInstance>();

            if (originalFileIds.get(cdl.TargetObjectId ) == null){
                cdlList2 = new List<ProcessInstance>();
                cdlList2.add(cdl);
                originalFileIds.put(cdl.TargetObjectId, cdlList2);
            }
            /* else if(originalFileIds.get(cdl.TargetObjectId) != null){
                cdlList2 = new List<ProcessInstance>();
                cdlList2 = originalFileIds.get(cdl.TargetObjectId);
                cdlList2.add(cdl);
                originalFileIds.put(cdl.TargetObjectId, cdlList2);
            }
            */
        }

        for (Id i: fileIds){
            //If have 1 or more attachments
            if (testmap.get(i) != null && originalFileIds.get(i) != null){
                testmap.get(i).Count_AA_Approvals__c = originalFileIds.get(i).size(); 

            //If have no attachments
            } else if (testmap.get(i) != null && originalFileIds.get(i) == null){
                testmap.get(i).Count_AA_Approvals__c = 0; 
            }
        }

        update testmap.values();
        System.Debug(testmap.values());

    } catch(Exception e){}
}

}

1 Answer 1

1

Yes, through the use of OOTB way along with some coding at trigger your use case can be solved.

Since, from approval configuration, either based on first response it is approved or all selected approvers should approve this.

So, you need to create 2 approval process and create a intermediate status field which might hold following values (Submitted for Approval, First Step Approval, Final Approval,Rejected) and approver name text field.

Both the approval process should have Approve or reject based on First Response.

Execution will be like this

  1. User will submit for approval and initial step of action will update the status to Submitted for Approval

  2. Any one of the approvers will approve this request. The workflow field update will update the status to First Step Approval and update approver name in the text field. Here this will fire a process builder and submit the approval for second approval process.

  3. Now 2nd approval request will go the all the approvers which you have defined. Here the first approver might received the same approval request. So, you need to put a check in the trigger that, approver name in that text field and current approver cannot be same. Means, same approvers cannot approve both the requests.

  4. Finally unique second approver will approve the request and update the status field to Final Approval.

Rejection part is easy to handle.

7
  • Say if the company decides to have at least 5 of the 10 approvers does that mean I need 5 approval processes?
    – compski
    Commented Oct 3, 2018 at 16:46
  • You could create a queue with those approvers. If 5 steps of approval is needed then you need to create 5 processes, there are no other way you can handle this scenario, I have faced in my experience. Commented Oct 3, 2018 at 17:08
  • Hmm I have tried creating a trigger to count the number of Status = 'Approved' and if counter = 5 then should auto submit for approval but can't seem to call the right objects. What about this way?
    – compski
    Commented Oct 4, 2018 at 1:32
  • Cant predict without seeing your code Commented Oct 4, 2018 at 5:30
  • ok bro updated my question
    – compski
    Commented Oct 4, 2018 at 13:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .