- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2021 10:12 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2021 10:23 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2021 10:23 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2021 12:18 PM
Ok, that helped a lot, thanks. I forgot that the "u_approval_type" field is on the "sysapproval_approver" table, not the "sysapproval_group" table. So I had to create another GlideRecord where after the script creates the Group Approval record, and ServiceNow creates the individual user approval records from that, that it updates that field.
So my code currently looks like this:
//create group approval
var grApproval = new GlideRecord('sysapproval_group');
grApproval.initialize();
grApproval.parent = current.sys_id;
grApproval.assignment_group = appGrp;
grApproval.approval = 'requested';
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();
}
I only seem to have one problem now.
Typically, when an individual user completes the approval, usually all other approval records (for other users) attached to that group approval are marked as "no longer necessary". Usually, this all happens without me having to do anything special (i.e. if I had created the Group Approval using the "Group Approval" workflow action). However, creating it this way by script does not appear to make this happen. I am guessing that there is some other setting I need to set when creating this record. Do you have any idea of what it is?
I will keep hunting around for it.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2021 12:51 PM
Hi,
Glad it helped, Please mark my response as correct as well.
Also you need to set 'wait for' field on group approval while inserting. Please add below line as per the requirement
grApproval.wait_for='any'; //for Anyone to approve
or
grApproval.wait_for='all'; //Everyone to approve
If I have answered your question please mark my response as correct and/or helpful.
Thanks,
Suseela P.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2021 01:11 PM
Hmmm... That did not seem to work out for some reason.
I checked all the setting on the ones that are created by the normal Group Approval action, and it looks like the "Wait for" setting is set to "Workflow".
Not sure if I still have to do something else.