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

There seem to be some issues with that approach.

 

In reality, our Custom table is structured more like this:

jmiskey_0-1698436386156.png

where the same group may be the approvers on multiple. 

 

So in this case, if Role1, Role4, and Role5 were selected, there should be 3 approvals.  However, the script you provided seems to only create one approval per group, so only 2 get created (instead of 3).  And the need one approval for each group, so they can approve some and deny others, if needed.

 

Also, if there are multiple approvals created, and the first one is Rejected, that kills the request instead of going on to the others (which is what we don't want).  It should only be fully rejected if all group approvals are rejected.

 

***UPDATE: I just talked to them, and convinced them to limit the Role Selection to just one per request (so change the Role Selection Variable from a List Collector to a Reference field).  That eliminates all this whole headache!  I am sure it will probably come up again someday, though.  But for now, we can proceed without it.