We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Create Group Approval in Workflow Run Script Action

jmiskey
Kilo Sage

We currently have a Run Script action in a workflow that dynamically creates a list of approvals based on a list of users.  It creates a separate approval for each person in the list.  It works quite well.  Here is what that code looks like:

//loop through approvers
			for (var j = 0; j < apps.length; j++) {
				var app = apps[j];

				//create approval
				var approval = new GlideRecord('sysapproval_approver');
				approval.initialize();
				approval.state = 'requested';
				approval.approver = app;
				approval.u_approval_type = 'Cognos: ' + pkg_name + ' Approval';
				approval.sysapproval = current.sys_id;   //Current is record that requires approval
				approval.insert();
			}

So "apps" is our list of users to create the approval for.

Now, I want to do the same sort of thing, but with Group Approvals instead.  So I want to create multiple Group Approvals for a list of groups that I have.  I imagine I could loop through each group, and then loop through each member in the group, and create an approval record for each one, but I think there probably is an easier way, if we can create a Group Approval record instead (and let ServiceNow handle the individual approvals part). 

I tried changing the table name in my GlideRecord to "sysapproval_group", and tried a few other changes, but it did not work for me (no errors, it just didn't create anything).  Does anyone know how to do something like above to create a Group Approval?

Thanks

 

1 ACCEPTED SOLUTION

Suseela Peddise
Kilo Sage

Hi,

You can try below code for generating grp approval

 for (var j = 0; j < apps.length; j++) {
        var app = apps[j];
        //Group approval
        var grApproval = new GlideRecord('sysapproval_group');
        grApproval.initialize();
        grApproval.setValue('parent', current.sys_id); //parent field refers to task table
        grApproval.setValue('assignment_group', app); //apps[j]
        grApproval.u_approval_type = 'Cognos: ' + pkg_name + ' Approval';
        grApproval.insert();
        workflow.info('Group approval has been created ' + grApproval.sys_id);
    }

If I have answered your question please mark my response as correct and/or helpful.

Thanks,

Suseela P.

View solution in original post

5 REPLIES 5

Hey Suseela,

 

Thanks for the code! But how can I create multiple "group" approvals if the groups are referencing a table I created that contains the groups, with users assigned to each group?