- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-15-2015 11:31 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2015 08:51 AM
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).
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2015 02:50 PM
Wow. Loved the solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2019 09:42 AM
This was a really useful find after a few tweaks to refer to my own definition table this worked a treat!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2020 12:58 PM
Hi Michael,
I know this post is already 5 years ago. I was hoping someone will reply to me. I have the same requirements like this one.
I have created a custom app and it requires me to have a dynamic sequential approvals.
I am new to scripting and I am kind a worried if the script in Activity 1 need to be change like the ManagementDocumentDB().
Hope you can help me thanks!
Mac
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2020 01:44 PM
Hi Mac,
All you need to understand about each activity in the example above is:
- Activity 1: Gathers the approver list, preferably in an array. The Document Management example above includes a sequence number as part of the array element. But, the way I've handled this in other processes I've built, I opted to set the sequence based on the position of the user in the array. For example, the user in pos[0] is the 1st approver, pos[1] the 2nd approver, and so on.
- Activity 2: Sets the approver based on the current sequence number. So you'd get the approver by array[current sequence number].
- Activity 3: Checks for additional approvers and increments the sequence number if there is another approver.
I hope this helps,
Michael
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2020 05:34 PM
Hi Michael,
Thank you for your quick response. I am trying to investigate the Activity #1 script and it calls a script include named DocumentManagementDB.
When you use this workflow from other processes did you copy the script include and change the information on the "approval_sequence" table and document table?