- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2017 12:41 PM
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.
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2017 12:52 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2017 12:52 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2017 01:33 PM
Thanks, Robert.
I used an Approval - User, assigned the group, and then added the condition based on script and entered the first script in the approval script field.
Thanks, again for your help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2018 02:34 AM
Hi All,
Can you anyone help me with a script for count based approval? If there are five members in CAB and 3 approval is required for approved and 1 reject for rejected. Please help.
Regards,
Jitendra Singh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2018 05:23 AM
if (counts.approved == 3){
answer = 'approved';
}
if (counts.rejected == 1){
answer = 'rejected';
}
*HIGHLY* recommend you don't script on raw number. All you need is for the membership of the group to change significantly and this won't make sense anymore.
Also, FYI, either the 3rd approval *OR* the 1st rejection will permanently move hte workflow beyond the approval.
Nothing will happen if the first rejection occurs 1 milisecond after the 3rd approval and vice versa.