Need help with Group Approval Script

Hernan3
Kilo Contributor

Disclaimer: I'm fairly new to ServiceNow and have limited knowledge in scripting, so please excuse my rookie status.

For our Change Request, we would like for 50% or more of the CAB members to approve before the task is marked approved. Currently, the task is marked approved if one member of the CAB group approves. I've tried using the scripts below and task is still marked approved after a single member of the CAB approves the change.

//Approve based on percentage indicated
var approvePercent = 50;
if((counts.approved/counts.total)*100 >= approvePercent){
      answer = 'approved';
}
if((counts.rejected/counts.total)*100 > (100 - approvePercent)){
      answer = 'rejected';
}

Below is another script I tried:

//Approve based on percentage from each group
var approvePercent = 50;
var approveCount = 0;
for(var id in groups){
      var group = groups[id];
      if((group.approved/group.total)*100 >= approvePercent){
            //Mark the group approval record 'Approved'
            var groupApproval = new GlideRecord('sysapproval_group');
            groupApproval.get(id);
            groupApproval.approval = 'approved';
            groupApproval.update();
            approveCount++;
      }
      if((group.rejected/group.total)*100 > (100 - approvePercent)){
            answer = 'rejected';
            break;
      }
}
//Ensure all groups have approved
if(approveCount == counts.total){
      answer = 'approved';
}

The other script I tried is attached. I modified the script since two groups are not required to approve, but still can't seem to make any progress.

Any guidance would be appreciated.

1 ACCEPTED SOLUTION

Uncle Rob
Kilo Patron

So this is a great way to learn the different nuances between Approval - User   and Approval - Group.   One of those nuances is that in Approval - Group "count.total" is the number of groups that have to approve.



In an Approval - Group, there's one approval record to be edited by the group.   You describe a scenario whereby different members of the "CAB Group" may have different opinions.   That scenario is best covered by an Approval - User which, paradoxically, can be assigned to a group (it just generates a seperate approval for each member of the group).



Using the Approval - User method, aiming it at the same group you've specified, the script you have above (the first one) should work.


View solution in original post

6 REPLIES 6

iamright
Giga Contributor

I've also used the same script.



//Approve based on percentage indicated


var approvePercent = 50;


if((counts.approved/counts.total)*100 >= approvePercent){


      answer = 'approved';


}


if((counts.rejected/counts.total)*100 > (100 - approvePercent)){


      answer = 'rejected';


}




Here's my question.   How do I make request "rejected" if any rejects?


You'd move a line from the Reject node of the Approval activity to a Set Stage activity where you specify Rejected, then move to the end of the Workflow.



find_real_file.png