Murthy Ch
Giga Sage

Hello Everyone,

In this article we will discuss how to manage the approval flow in approval group activity based on the script. Recently we had a situation where we have to give a chance to the approvers to approve at least 50%(half of the users) from the total approvers.

Let's say for example we have a group of 10 users in that at least 5 of them has to be approve and if any one rejects the approval reject the entire group approval and end the workflow. I did a search in community but nothing relevant answers found so I decided to write this small article.

We can write the below logic in Approval script section:

for (var id in groups) { //used to iterate the each group
    var objGroup = fncGetGroupObj(id); //calling the function
    var strGroupName = objGroup.name; //use this only if you want to compare according to group names
    var group = groups[id];
    if (group.total > 1) { // checking approvers count for each group
        if (parseInt(group.total / 2) == group.approved) { //checking the condition for half of the approvers
            fncSetGroupState(id, "approved"); //calling the function
        } else if (group.rejected == 1) {
            fncSetGroupState(id, "rejected"); //calling the function
        }
    } else if (group.total == group.approved) { // if the group has only one approver
        answer = "approved";
    } else if (group.rejected == 1) { //if any one rejects
        answer = "rejected"; //rejecting the entire approval flow
    }
}
if (counts.requested == 0) { //if none of the group approval is not in requested state
    if (counts.approved == counts.total) // if total group approvers and approved groups are equal approving the entire flow
        answer = "approved";
    else //if not same
        answer = "rejected";
}
//the below function is used to update the group approval record
function fncSetGroupState(sidGroupApproval, state) {
    var objGroupApproved = new GlideRecord('sysapproval_group');
    objGroupApproved.get(sidGroupApproval);
    objGroupApproved.approval = state;
    objGroupApproved.update(); //updating the group approval state
}
// the below function you can find it in DOCS
function fncGetGroupObj(sidGroupApproval) { //used this to get the group name 
    var objGroupApproval = new GlideRecord('sysapproval_group');
    objGroupApproval.get(sidGroupApproval);
    var objGroup = new GlideRecord('sys_user_group');
    objGroup.get(objGroupApproval.assignment_group.sys_id);
    return objGroup;
}

The above code is may vary as per your business requirements. Please take it as a reference.

Please feel free to ask me questions if you have any in comments section.

Happy to help:)

 

Thanks,

Murthy

Comments
Ashish113
Tera Contributor

Hi Murthy,

Thanks for the solution, for me it is not working until I update the answer variable after calling setstate function and answer variable should be updated only after calling that function. If I updated that before calling that function it is not working. If you know why please respond

Thanks,

Ashish Katakam

Jakkamsetti Pra
Tera Contributor

Hi Murthy,

Awesome, it's perfectly working for me. I did some changes as per the business requirement. Before looking into your code I did from end, but it worked very partially. After looking your code, I have taken your code as reference and did some changes, it perfectly worked.

Thank you.

Murthy Ch
Giga Sage

Glad it helped! @Jakkamsetti Pra 

Version history
Last update:
‎06-07-2022 10:45 PM
Updated by: