Creating Group Approvals in a Workflow Via Script Not Behaving Properly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2021 08:01 AM
Typically, we use the Group Approval action in the Workflow to create group approvals. However, we have Catalog Items where users can select more than one "role" to request, and each "role" has its own Approval Group, which is maintained by a Role Data Dictionary table. So we have a Run Script action, which loops through all the Roles they have selected, and creates a separate Group Approval for each one. The code looks something like this:
//capture roles selected from variable
var roleList = current.variables.role.toString();
//loop through all roles
var roleArray = roleList.split(',');
for (var i = 0; i < roleArray.length; i++) {
var rol = roleArray[i];
var gr = new GlideRecord('u_tango_roles');
gr.addQuery('sys_id', rol);
gr.query();
//create group approvals
with (gr.next()){
if (gr.u_approval_needed == true){
var appGrp = gr.u_role_approver;
var role_name = gr.u_role_name;
//create group approval
var grApproval = new GlideRecord('sysapproval_group');
grApproval.initialize();
grApproval.parent = current.sys_id;
grApproval.assignment_group = appGrp;
grApproval.approval = 'requested';
grApproval.wait_for='any'; //wait for anyone to approve
grApproval.insert();
//add approval types to approver approval records
var apprv = new GlideRecord("sysapproval_approver");
apprv.addQuery("sysapproval", "=", current.sys_id);
apprv.addQuery("state", "=", 'requested');
apprv.addQuery("u_approval_type", "=", "");
apprv.query();
while (apprv.next()) {
apprv.u_approval_type = 'Tango: ' + role_name + ' Approval';
apprv.update();
}
}
}
}
So, this script does correctly create the Group Approval records, and the accompanying Approver Approval records, for each member in the groups.
It does what it is supposed to, except for one big thing. Even though we have the "Wait For" field set to "any" (so that once one person approves or rejects an approval, all other approval records created for that particular approval are deemed unnecessary), it is not working properly. If one person approves a particular approval, all the other persons in that group still have their individual approval records showing as "Requested" instead of "No Longer Required".
I compared these approvals created by scripts against one that are created by the Group Approval action (and behave like they should), and could not find any differences. In both cases, the following values on the Group Approval record are the same:
- Wait for: Anyone to approve
- Upon approval: Proceed to next task
- Upon reject: Cancel all future tasks
What am I doing wrong? Am I missing some sort of setting?
How do I get the Group Approval (accompanying user approval records created) created by a Run Script action to behave in the same manner as one created with the Group Apprval action?
Thanks
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2021 10:34 AM
use the below script in the Approval group activity, choose anyone to approve or reject option in the activity
var ans=[];
//Pushing the group
var roleList = current.variables.role.toString();
var approvers=[];
//loop through all roles
var roleArray = roleList.split(',');
for (var i = 0; i < roleArray.length; i++) {
var gr = new GlideRecord('u_tango_roles');
gr.addQuery('sys_id', roleArray[i]);
gr.query();
//create group approvals
while(gr.next()){
if (gr.u_approval_needed == true){
approvers.push(gr.u_role_approver));
}
}
}
var app = approvers.toString();
answer.push(app);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2021 10:55 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2021 11:01 AM
can you try the below?
var answer=[];
//Pushing the group
var roleList = current.variables.role.toString();
var approvers=[];
//loop through all roles
var roleArray = roleList.split(',');
for (var i = 0; i < roleArray.length; i++) {
var gr = new GlideRecord('u_tango_roles');
gr.addQuery('sys_id', roleArray[i]);
gr.query();
//create group approvals
while(gr.next()){
if (gr.u_approval_needed == true){
answer.push(gr.u_role_approver.toString()));
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2021 11:20 AM
I had to remove the extra ")" you had in the approver "push" lines in each code, but once I did that in this second code, it did not error anymore. However, it didn't really do what I needed either.
If I selected two different roles, so it should be creating two different group approvals, it did not create the second one, just the first one.
Also, it does not populate the Approval Type value on each of the sysapproval_approver records, like I need. I tried amending your code to get it to do that, but it did not work.
Here is what I tried there:
var answer=[];
//Pushing the group
var roleList = current.variables.role.toString();
var approvers=[];
//loop through all roles
var roleArray = roleList.split(',');
for (var i = 0; i < roleArray.length; i++) {
var gr = new GlideRecord('u_tango_roles');
gr.addQuery('sys_id', roleArray[i]);
gr.query();
//create group approvals
while(gr.next()){
if (gr.u_approval_needed == true){
answer.push(gr.u_role_approver.toString());
//add approval type to sysapproval_approver records
var apprv = new GlideRecord("sysapproval_approver");
apprv.addQuery("sysapproval", "=", current.sys_id);
apprv.addQuery("state", "=", 'requested');
apprv.addQuery("u_approval_type", "=", "");
apprv.query();
while (apprv.next()) {
apprv.u_approval_type = 'Tango: ' + gr.u_role_name + ' Approval';
apprv.update();
}
}
}
}
But even without that code I added, it still will not create more than one Group Approval record, like I need (which is one of the reasons why I was using a script in the first place).