THURSDAY, JANUARY 19, 2017

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

Looking for other approval script examples? Check out this article about change management approval scripts!

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

10 Comments

Jeff Jefferson 07-11-2013, 14:53

I have a question about this, We use an additional approves script for approval, so for instance we have set up in change module that it could be one of three groups doing the approvals, not all at the same time, each group has a different number of people associated with it, Everything works great right now but it set to 1 Person approves the rest aren’t needed, we want to set this style up for a percentage of 60% approved sets Status to approved or 60% rejected sets the status to Closed, will this work for that type of approval?

Reply
Mark Stanger 07-11-2013, 14:57

These scripts do pretty much what you’re describing with one difference. The percentage indicated is for approval, not rejection. If you do what you’re describing you would end up with cases where the percentage wasn’t met either way so the workflow wouldn’t go anywhere.

Reply
Jeff Jefferson 08-11-2013, 06:56

OK tried this script but it errors out saying Group ID not defined, any thoughts?

Reply
Mark Stanger 08-11-2013, 08:07

I’m not sure what would cause that, but it sounds like you’ve probably got something else in our environment that is interfering. The scripts here don’t even reference anything named ‘Group ID’. You could identify if it was something in your config or something in ServiceNow by setting the same thing up in a ServiceNow demo instance to test.

Reply
Jeff Jefferson 08-11-2013, 08:27

Thanks Mark, got it worked out, I was using the group script but should have been using the user one, Even though we are using groups, not all the groups approve or reject. Thanks for your wisdom and thanks for all the posts you do, People like me really appreciate you.

Reply
Mark Stanger 08-11-2013, 08:29

Thanks! I’m glad you got it working.

Reply
Waqas Mahmud 05-03-2014, 14:40

Hello – how can I setup if my ECAB Approval-User has 4 Members (Lets say A, B, C, D)

If A approves then B, C, D go to No Longer Required

else if

B, C, D approve then A should go to No Longer Required

Any help on how to get started here?

Reply
Dave Edgar 05-05-2016, 04:57

Hi Mark

I need something very similar to Waqas i think.

My scenario is that I have approvals set at a value. So approval for <25K could come from one of two groups. Group one is level 2 users and group two are level 1 users (more important. So;
for the 1st group to grant approval 2 users must have approved
OR
for the 2nd group to have approved at least on members of the group must approve.

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

Dave

Reply
Neeraj sharma 26-12-2016, 22:45

I want to do similar kind of thing, I want ‘CAB’ approval of user A mandatory if he approves other approvals no longer required or approval should continues until user A approves.
(user A is also a part of ‘Change manager’ group).

Can you please help
thanks in advance

Reply
Mark Stanger 27-12-2016, 10:56

Check out the ‘Making a specific person the key approver’ section in this Guru post…
http://www.servicenowguru.com/scripting/change-management-workflow-approval-scripts-servicenowcom/

Reply

Leave a Reply


Latest Comments

  • David: It appears that I can hit sys_properties table with REST. This works, but I haven’t yet discovered the...
  • Mark Stanger: Hey David, It doesn’t surprise me that scoped apps have made this more difficult. I’m not...
  • David: Mark, do you have an example of how to do this in a scoped app? It seems there are many hoops to jump through...
  • Mark Stanger: The only possibility is to create a system property to override this in your application. Check out the...