Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Approvals are not set to "No longer required"

MS17
Tera Contributor

Hi All,

I have a requirement that when group approval is triggered for a request, and if any new members added to that group, then that users should also see the approval record.

I have created a after BR on sys_user_grmember table, currently when request is created and group approval triggered and afterwards when i add new users to that group, the users could see the approval record. the issue is when the request is approved by anyone of the group member, other approval records are moved to "no longer required", but newly inserted approvals on sysapproval_approver are not moved to "no longer required", still is in "requested" state.

Any help would be appreciated. Thank you.

 

 

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

    var groupId = current.group;
    var userId = current.user;

    //active group approvals
    var appr = new GlideRecord('sysapproval_group');
    appr.addQuery('assignment_group', groupId);
    appr.addQuery('approval', 'requested'); // only pending approvals
    appr.query();

    while (appr.next()) {


        // approval entry for the new user
        var grpAppr = new GlideRecord('sysapproval_approver');
        grpAppr.addQuery('group', appr.sys_id);
        grpAppr.query();
        if (grpAppr.next()) {
            var newAppr = new GlideRecord('sysapproval_approver');
            newAppr.initialize();
            newAppr.sysapproval = appr.parent;
            newAppr.group = appr.sys_id;
            newAppr.state = appr.approval;
            newAppr.approver = userId;
            newAppr.document_id = grpAppr.document_id;
            newAppr.source_table = grpAppr.source_table;
            newAppr.insert();
        }

    }


})(current, previous);
1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

If you have a (legacy) workflow with an Approval - Group activity, you'll need to put that within an Approval Coordinator activity to also handle the manual approvals that you are creating with the BR.  I'm not sure if there's an equivalent action necessary for Flow Designer/Workflow Studio.  Also take a look at the createUserApprovals function in the WorkflowApprovalUtils Script Include that the out of box Business Rule calls when a group approval is created to create the user approvals.  Maybe one of these other fields on the approval record that's created is needed by the BR that is updating the State of the others.

var ids = this.getMembersOfGroup(groupApproval.assignment_group);
      var state = groupApproval.approval + '';
      var taskId = groupApproval.parent + '';
      var approvalId = groupApproval.sys_id + '';

      for (var i = 0; i < ids.length; i++) {
    	 var approval = new GlideRecord('sysapproval_approver');
         approval.initialize();
         approval.sysapproval = taskId;
         // fill out reference to task with the in-memory GlideRecord. 
         // When "Approval Events (Task)" runs it will be able to obtain the task type even though it doesn't yet exist in DB
         var target = groupApproval.parent.getRefRecord();
         approval.sysapproval.setRefRecord(target);
         approval.source_table = target.getRecordClassName();
         approval.document_id = target.sys_id +'';
         approval.group = approvalId;
         approval.approver = ids[i];
         approval.wf_activity = groupApproval.wf_activity;
         approval.state = state;
         if (state == 'approved')
            approval.setValue('comments', gs.getMessage('Automatic approval'));
         
         approval.expected_start = groupApproval.expected_start;
         approval.due_date = groupApproval.due_date;
         approval.insert();
      }