Create Group Approval from Workflow Run Script Action

jmiskey
Kilo Sage

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:

jmiskey_0-1698432583857.png

And here are the parent Group Approval records:

jmiskey_1-1698432659272.png

 

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.

5 REPLIES 5

Jaspal Singh
Mega Patron
Mega Patron

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.

Brad Bowman
Kilo Patron
Kilo Patron

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.

[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:

jmiskey_0-1698434108432.png

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

 

 

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());
	}
}