Dynamic number of approvals

Michael Domke
Tera Guru

Wondering if anyone has done anything with regards to dynamically handling the number of approvals in a single workflow. The idea is that something needs to go through a series of inline approvals, meaning each approval is created after the previous one has been approved, yet the number of approvals is dynamic based on various conditions of what's being requested.

So, one request may require two approvals yet another request may require four approvals. Again, the approvals must be inline so I don't believe I can use a group-type approval activity since all approvals would be sent at the same time.

My idea so far is to add several approval activities to the workflow and use them as placeholders. For example, I would add 10 approval activities to the workflow but only use two of them if the request required two approvals, or use four of them if the request required four approvals. The drawback to this approach is if the request required more approvals than there are placeholders.

Any other ideas?

Thanks,

Michael

1 ACCEPTED SOLUTION

Michael Domke
Tera Guru

I appreciate all the replies buy my issue is not dynamically adding one or more approvers to a single Approval workflow activity. What I'm looking for is a way to dynamically add sequential approvals. That is, approval #1 must be approved before approval #2 is sent, and #2 must be approved before #3 is sent and so on. The key is the number of approvals must be dynamic, for example, one request may only require 2 approvals whereas another request may require four approvals - again, in sequence not in parallel.



So I turned to a ServiceNow friend, Dave (Super Dave) Knight and he pointed me to the ServiceNow Document Management Application (requires a plugin activation) which already has this dynamic sequential approval feature in place.



Here's how it works (courtesy of Super Dave):



There is a list of approvers at the bottom of the form, arbitrary length.   There is also a field on each approver called 'Sequence'.   If you are #1, you approve first.   If you are #2, second, and so on.   Better yet, if two people are at #2, they approve in parallel, after #1 approvers, and before #3.   You get the picture.




There is a workflow built to handle that.   It is designed to iterate approvals through that list.   It is called 'Document Management Default', illustrated below.   It uses a standard Approval activity (scripted of course), and a loop.   I've included the code in the key activities marked 1, 2 and 3 below (in case you do not have the document management plugin installed).



Doc Mgmt Workflow.png


Activity #1 (Run Script): Basically goes to the list of records in the DB and remembers the who and sequence number for each value in RAM (the scratchpad).   The code that gets the list out of the DB is in DocumentManagementDB, but as you will see, all it does is create an array of approvers, where each object has the name and sequence number of the approver.



current.approval = 'requested';


var db = new DocumentManagementDB();


var approvers = db.getApprovers(current.document);


workflow.scratchpad.approvers = approvers;


if(approvers.length>0){


  workflow.scratchpad.current_sequence = approvers[0].sequence;


} else {


  workflow.scratchpad.current_sequence = 0;


}



Activity #2 (Approval — User, Additional approvers script): This just gets the array of all approvers at the current sequence number …



answer = [];


var currentSequence = workflow.scratchpad.current_sequence;


var approvers = workflow.scratchpad.approvers;


for(var i=0; i<approvers.length; i++){


  if(approvers[i].sequence == currentSequence) {


      if(approvers[i].user){


          answer.push(approvers[i].user);


      } else {


          answer.push(approvers[i].group);


      }


  }


}



Activty #3 (If, Script):




answer = isThereANextSequence();


function isThereANextSequence() {


  var currentSequence = workflow.scratchpad.current_sequence;


  var approvers = workflow.scratchpad.approvers;


  var nextSequence = getNextSequence(approvers, currentSequence);



  if (nextSequence>0) {


      workflow.scratchpad.current_sequence = parseInt(nextSequence);


      return 'yes';


  }


  return 'no';


}



function getNextSequence(approvers, currentSequence){


  for(var i=0; i<approvers.length; i++){


      if(approvers[i].sequence>currentSequence){


          return approvers[i].sequence;


      }


  }



  //We went through all the sequences


  return 0;


}



I know I'm going to find many uses for this functionality and I hope others find it useful as well.



Once again, many, many thanks to Dave Knight for this information.



Michael


View solution in original post

28 REPLIES 28

randrews
Tera Guru

I had a very similar requirement in server decom... had to get an approval from each of the applications on the server with a max of ten....... the only difference being mine was in parallel not in series...



what i did was to create an array for the application names and push the apps in...



then i setup the approvals with an IF in front of them.. and if array.length <2 i sent approval 2.. ❤️ i sent approval 3 etc etc etc...



then in the approvals themselves i looked up the app rovers based on the app name...


i can see you doing the same thing on yours... so for example say you are using multiple approvals by mutiple people each...



each set of approvers would be separated with a ; and each GROUP of approvals by a |


so your string array would look like   grip1approrover1;grp1approer2;grp1apprvor3|grp2apprvover1;grp2approer2|grp3apprver1



now you can split the array by | into group_array... and use group_array.length to see if you need to send the approvals or not... then inside each of the approvals you can do a split on ; to get the individual approves for that group.


shembop
Tera Contributor

Raymond,


Reading through this sounds like you did something clever. Maybe you can wrap your head around this problem.



https://community.servicenow.com/message/918347?et=watches.email.thread#918347


poyntzj
Kilo Sage

Have a look at this as it may help


Advanced Approval Groups



We use it here for complex workflows and approval processes.


The benefit is that you can choose if an approval group is one, all, percentage or count


in essence you set a few variables


you call a sub workflow and pass some data across


It then looks at an u_advanced_approvals table and finds any entries that match the criteria


it then requests approvals and updates as it goes



You can call as many sub workflows as needed, in parallel or series.



You can then expand the u_advanced_approvals table as you need


danielbilling
Kilo Guru

For standard request items we have created 3 fields on each item.


Approval step 1, 2 and 3. this is a dropdown list with values (similar to Item creator in Eureka)...like Line manager approval, Service Owner approval, CI approval...etc


The workflow is checking the ordered items approval step field and uses the Approval coordination function to decide the approval group/user.



Hope this short info helped