logo

NJP

Percentage or Majority-based Workflow Approvals

Import · Jan 31, 2013 · article

Home/Graphical workflow/Percentage or Majority-based Workflow Approvals

One very common service request or change request approval requirement is to ask for a percentage or majority-based approval. This is something that ServiceNow workflow can do, but it requires a bit of scripting. In the following article, I’ll show you how you can set up some simple scripts in your graphical workflow ‘Approval’ activities to handle these scenarios for any percentage you choose.

ServiceNow Majority Approval

The following example scripts are designed to be placed directly in the ‘Approval Script’ field on a workflow ‘Approval’ activity as shown in the screenshot above. The ‘Approval Script’ field is only visible if you select ‘Condition based on script’ from the ‘Wait for’ field. Once you paste the script in, just adjust the ‘approvePercent’ variable for the specific approval percentage needed.

Percentage-based approval with an ‘Approval – User’ activity

This first example assumes you’ve got a group of users grouped into a single workflow activity. These users could all be members of a single group. In fact, this is how I typically do majority approvals for the Change Advisory Board. Regardless of whether the users come from a group or are added individually, they are all combined together when evaluating counts in an ‘Approval – User’ activity.

//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';

}

Percentage-based approval with an ‘Approval – Group’ activity

It is also possible (albeit a bit more complex) to do percentage-based approvals with an ‘Approval – Group’ activity. The primary difference here is that you can require a certain percentage from multiple groups. In the example given, each individual group must meet the 50% approval threshold before the activity will return ‘Approved’ and tell the workflow to advance. Any single group not meeting the 50% mark will force a rejection for the entire activity. This took me way more time than it should have to figure out :). Hopefully it saves you a bit of time.

//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';

}

Can this be done? I'm sure it can with complex javascript, which is why i'm here.

Dave

View original source

https://web.archive.org/web/20210302040738/https://servicenowguru.com/graphical-workflow/approval-percentage/