Create Group Approval from Workflow Run Script Action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 11:54 AM
We have a "Run Script" workflow action that loops through the values in a List Collector field (based on a Custom Table that lists Roles & Approval Groups), and creates a Group Approval for each selection.
That code looks like this:
//capture packages selected from variable
var roleList = current.variables. role_selection.toString();
//loop through all packages
var roleArray = roleList.split(',');
for (var i = 0; i < roleArray.length; i++) {
var role = roleArray[i];
var gr = new GlideRecord('x_ebcbs_workday_ad_workday_administration_user_based_roles');
gr.addQuery('sys_id', role);
gr.query();
//create approvals
if (gr.next()){
var appGrp = gr.approval_group;
var role_name = gr.role_name;
//create approval
var approval = new GlideRecord('sysapproval_group');
approval.initialize();
approval.state = 'requested';
approval.approval = 'requested';
approval.assignment_group = appGrp;
approval.short_description = role_name;
approval.wait_for = 'any';
approval.parent = current.sys_id; //Current is record that requires approval
approval.insert();
}
}
It seems to work fine. However, I am having an issue when working these.
The Group Approval automatically creates individual User Approval records for each user. As a user approves or rejects the approval, the workflow proceeds on as expected. However, it still continues to show the original Group Approval in a "Requested" state. So it does not appear that the approvals/rejections the users are completing are updating the Group Approval record.
Here are the indovidual Approver records:
And here are the parent Group Approval records:
So why aren't the updates to the individual approver records updating the parent Group Approval records? If I create a Group Approval via the "Approval - Group" action, they seem to be linked properly and update as expected. But if I create the Group Approvals via the script I show, they do not seem to behave it that manner.
Am I missing some setting in my script?
I tried changing:
approval.wait_for = 'any';
to
approval.wait_for = 'workflow';
but that did not seem to make any difference.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 12:04 PM
Hi,
Since, you are creating Group approval via scirpt instead of using Approver-Group activity you might have to GlideRecord the sysapproval_group table once the approval action is performed and update the record accordingly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 12:09 PM
Why would/do you need to try to replicate what the Approval - Group activity already does? Why not add a similar script to that activity instead? When a record is inserted on the sysapproval_group table the SNC - Create user approvals for group Business Rule calls a Script Include to create the individual approvals, so you could take a look at that to see if there's a relation back to the group that is not getting set with your script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 12:17 PM
[quote]Why would/do you need to try to replicate what the Approval - Group activity already does? Why not add a similar script to that activity instead? [/quote]
I would love to do something like that, I am just not sure what something like that would look like.
For simplicity sake, let's say that our Custom Roles Table looks something like this:
So, on the List Collector on the Catalog Item, then can select as many roles as they want. Let's say that they select Role1, Role3, and Role5. Then in my workflow, I need to dynamically create 3 different Group Approvals, for Group1, Group3, and Group5.
Is there an easy way to create an unknown number of approvals that does not involve scripting it out like I tried?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 12:25 PM
The scripted approach should be fine. Inside the Advanced script on the Approval - Group activity, push the group sys_id to the answer more like this:
var answer = [];
//capture packages selected from variable
var roleList = current.variables. role_selection.toString();
//loop through all packages
var roleArray = roleList.split(',');
for (var i = 0; i < roleArray.length; i++) {
var role = roleArray[i];
var gr = new GlideRecord('x_ebcbs_workday_ad_workday_administration_user_based_roles');
gr.addQuery('sys_id', role);
gr.query();
//create approvals
if (gr.next()){
answer.push(gr.approval_group.toString());
}
}