User(s) approval - Wait for Condition based on script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2019 11:32 AM
I have a form that collects different "Programs" in a list collector field that references a program table with approvers for each program. Each program has one or more approvers.
I want to create an "Approval - User" activity in the workflow that will require ONE approval from each program. How can I go about this? I basically need the same functionality as the "Wait for An approval from each group" in the "Approval - Group activity" I need this because my requirements don't allow me to create groups for each program.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2021 01:56 AM
Hi,Did you got solution to above requirement?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2021 03:18 AM
Hello
I was able to achieve this requirement by following the steps below -
1) Created a new field on sysapproval_approver table to store the group name
2) Replaced my Group approval activity in the workflow with three new activities as shown in the screenshot
Run Script - To trigger approvals to users
current.u_approval_flag = 'CI';
var ci = new GlideRecord('task_ci');
ci.addQuery('task', current.sys_id);
ci.query();
while (ci.next()) {
var grp = ci.ci_item.assignment_group.getDisplayValue();
var manager = ci.ci_item.assignment_group.manager;
if (manager != '') {
createApproval(manager, grp);
}
var director = ci.ci_item.assignment_group.u_director;
if (director != '') {
createApproval(director, grp);
}
}
function createApproval(approver, group) {
var app = new GlideRecord('sysapproval_approver');
var changetable = 'change_request';
app.initialize();
app.approver = approver;
app.state = 'requested';
app.sysapproval = current.sys_id;
app.source_table = changetable;
app.approval_column = changetable.approval;
app.approval_journal_column = changetable.approval_history;
app.document_id = current.sys_id;
app.u_ci_group_name = group; // Newly created field to store group name in approval table
app.insert();
}
Wait for Condition - To wait until if all the approvals are moved out of requested state or one of the approvals are rejected
array = [];
var app = new GlideRecord('sysapproval_approver');
app.addQuery('sysapproval', current.sys_id);
app.query();
while (app.next()){
array.push(app.state.toString());
}
workflow.scratchpad.list = array.toString();
if (array.indexOf('requested') == -1 || array.indexOf('rejected') >= 0)
{
answer = true;
}
Approved (If condition) - It checks if approvals are approved or not, then proceeds with activities it needs to follow
var array2 = workflow.scratchpad.list;
answer = ifScript();
function ifScript() {
if (array2.indexOf('rejected') >= 0) {
return 'no';
} else if (array2.indexOf('requested') == -1 && array2.indexOf('approved') >= 0) {
return 'yes';
}
}
Business rule - To move approval records on a ticket to either Rejected or No longer required.
(function executeRule(current, previous /*null when async*/ ) {
if (current.state.changesTo('approved')) {
var app = new GlideRecord('sysapproval_approver');
app2.addQuery('sysapproval', current.sysapproval);
app.addQuery('u_ci_group_name', current.u_ci_group_name);
app.addQuery('u_ci_group_name', '!=', '');
app.addQuery('sys_id', '!=', current.sys_id);
app.query();
while (app.next()) {
app.state = 'not_required';
app.update();
}
}
if (current.state.changesTo('rejected')) {
var app2 = new GlideRecord('sysapproval_approver');
app2.addQuery('state', 'requested');
app2.addQuery('sysapproval', current.sysapproval);
app2.addQuery('u_ci_group_name', '!=', '');
app2.addQuery('sys_id', '!=', current.sys_id);
app2.query();
while (app2.next()) {
app2.state = 'not_required';
app2.update();
}
}
})(current, previous);
Hope these information helps you get your requirement done. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2021 01:36 AM
Thanks a lot. I will go through this and let you know the result.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2021 07:17 PM
Hello. Is it possible to do this with group approvals? I already have a script for group approvals, but do to the limitations of the group approval activity (adding groups on the fly), I'd like to use the Wait For script instead. This is what I had for the group approval script, but having trouble moving it to the Wait For script.
var cgr = '';
var objGroup = '';
var strGroupName = '';
var intNA=0;
var intR=0;
var intA=0;
var approval_figure = 0;
var gappgr = '';
for (var id in groups)
{
//gs.log(intR + ' : ' + intA + ' : ' + intNA);
strAppGroup = fncGetAppGroupObj(id);
objGroup = fncGetGroupObj(id);
strGroupName = objGroup.name;
strGroupID = objGroup.sys_id;
gs.log('approval Count : ' + strGroupName + ' : ' + strGroupID + ' : ' + strAppGroup);
var cgr = new GlideRecord('x_reviewer_group');
cgr.addQuery('name',strGroupID);
cgr.query();
while (cgr.next())
{
approval_figure = 0;
approval_figure = cgr.approve_count;
gs.log('approval group count approval group : ' + strGroupName + ' : ' + cgr.name + ' : approved : ' + groups[id].approved + ' : total : ' + groups[id].total + ' : approval figure : ' + approval_figure);
if (groups[id].rejected >= 1)
{
intR++;
}
else if (groups[id].approved >= approval_figure)
{
intA++;
gappgr = new GlideRecord('sysapproval_group');
gappgr.addQuery('number',strAppGroup);
gappgr.query();
if (gappgr.next())
{
gappgr.approval = 'approved';
gappgr.update();
}
}
else
{
intNA++;
}
}
}
gs.log('rejected : ' + intR + ' : Not Approved : ' + intNA+ ' : Approved : ' + intA);
if (intR > 0)
{
answer = 'rejected';
}
if (intNA == 0 && intR == 0)
{
answer = 'approved';
}
function fncGetGroupObj(sidGroupApproval)
{
var objGroupApproval = new GlideRecord('sysapproval_group');
objGroupApproval.get(sidGroupApproval);
var objGroup = new GlideRecord('sys_user_group');
objGroup.get(objGroupApproval.assignment_group.sys_id);
return objGroup;
}
function fncGetAppGroupObj(sidGroupApproval)
{
var objGroupApproval = new GlideRecord('sysapproval_group');
objGroupApproval.get(sidGroupApproval);
return objGroupApproval.sys_id;
}