How to set approval state to No Longer Required if a group member meets certain conditions?

thrsdy1512
Tera Expert

If a record on the change task table is assigned to a user who is also a member of an approval group that is linked to the change request, I want the approval state for that Change request to be 'No Longer Required' for that specific user only. 

I have created a business rule on the approvals table which runs on insert however I can't get it to work. 

 

new change is logged > a change task (type = peer review) is created as part of it > the change task is assigned to user #1 > the change progresses and triggers approval > in scenarios where user #1 is also member of the approval group the approval state for them is no longer required & work note added to say they can't approve as they are assigned to the change task> other members of the approval group still need to approve the record.

 

My script is:

(function executeRule(current, previous /*null when async*/) {
   
    var sourceTable = current.change_request;
    var sourceRecord = new GlideRecord(changeRequest);
    sourceRecord.get(current.sys_id);
   
    var changeTaskGR = new GlideRecord('change_task');
    changeTaskGR.addQuery('change_request', sourceRecord.sys_id);
    changeTaskGR.addQuery('type', 'peer review');
    changeTaskGR.query();

    while (changeTaskGR.next()) {
        var peerReviewAssignee = changeTaskGR.assigned_to;
       
        var approvalGroupGR = new GlideRecord('sys_user_grmember');
        approvalGroupGR.addQuery('group', current.group);
        approvalGroupGR.query();
       
        while (approvalGroupGR.next()) {
            if (approvalGroupGR.user == peerReviewAssignee) {
                current.state = 'no longer required';
                current.update();
            }
        }
    }
   
})(current, previous);
7 REPLIES 7

Mark Manders
Mega Patron

This BR is on the sysapproval_approver table? In that case your first line doesn't return anything, since there is no 'change_request' on the current record. 

Also, your second line checks for a non existing table. It should be 'new GlideRecord('change_request'); 

 

You are never getting the change tasks back. 

 

Why not make it easy on yourself and create a flow for this? No need to script anything. Just drag and drop.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

A flow would certainly be better! I am unsure of how to get the flow to  check if the change task assignee against the approval group members for the change request that the task is linked to. Would the trigger be the change task being created or the change request being created? As the flow needs to look at the request and the task both.

 

Ankur Bawiskar
Tera Patron
Tera Patron

@thrsdy1512 

your script is not correct

1) field on Approval table which holds CHG is sysapproval

2) you can query change_task with setLimit(1) to search only for 1 record

3) it's before insert so you don't need current.update();

Ensure your before insert BR runs only for CHG and not for any other tables

BR Condition:

current.source_table == 'change_request'

Script:

(function executeRule(current, previous /*null when async*/ ) {

    var sourceRecord = new GlideRecord('change_request');
    if (sourceRecord.get(current.sysapproval)) {
        var changeTaskGR = new GlideRecord('change_task');
        changeTaskGR.addQuery('change_request', sourceRecord.sys_id);
        changeTaskGR.addQuery('type', 'peer review');
		changeTaskGR.setLimit(1); // search only 1 record
        changeTaskGR.query();
        if (changeTaskGR.next()) {
            var peerReviewAssignee = changeTaskGR.assigned_to;
            var approvalGroupGR = new GlideRecord('sys_user_grmember');
            approvalGroupGR.addQuery('group', current.group);
			approvalGroupGR.addQuery('user', peerReviewAssignee);
            approvalGroupGR.query();
            if (approvalGroupGR.hasNext()) {
                    current.state = 'no longer required';
            }
        }
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I have modified the script and was still unsuccessful. Will try attempting it as a flow as suggested by another contributor.