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
06-07-2024 05:53 AM
It only ever does one at a time!
It is adding one to "answer" with each loop!
So you should see the multiple "badge approval" log statements, one for each iteration of the loop.
I am not sure if "approval" is some sort of reserved word it doesn't like you using or not, so let's change that to see if that makes a difference. Also, let's add a counter inside of our approval creation, and add a few log statements at the end to count the total number of loops and approvals.
var answer = [];
var ct = 0;
//capture Badge Locations selected from variable
var badgeList = current.variables.select_plant_access_select_all_that_apply.getDisplayValue().toString();
gs.log(badgeList, "Badge Locations");
//loop through all Locations
var badgeArray = badgeList.split(',');
for (var i = 0; i < badgeArray.length; i++) {
var badge = badgeArray[i];
gs.log(badgeArray, "Locations Array");
gs.log(i, "Badge i");
var gr = new GlideRecord('u_badge_doors_updated');
gr.addQuery('u_location', badge);
gr.query();
//create approvals
while (gr.next()) {
ct++;
var appr = gr.u_approval_groups;
answer.push(appr.toString());
gs.log(appr, 'badge approval');
}
}
gs.log(i, 'Total Loop Count');
gs.log(ct, 'Total Approval Count');
What do the final two log statements at the end of your code return when run?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 07:15 AM
Total Loop Count resulted in "2." I only selected 2 locations on the catalog item form.
Total Approval Count resulted in "393," all having the same sys_id that seemed to capture the first location I selected on the catalog item.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 08:02 AM
Are you trying to create multiple Group Approvals for the same group?
If all the records you are adding to "answer" have the same value, I think it may just create one Group Approval (see there is only one unique value repeated many times).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 08:19 AM
No, I'm trying to create multiple Group Approvals based on the locations selected. For example, if Location A, B, C and D are selected, then create a group approval for each Location.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 08:24 AM
But who are you assigning them too? Are you assigning them all to the same approval group?