Updating group approval sysapproval_group

servicenow14710
Tera Expert

Hello developers, 

 

i am facing a issue with group approvals where multiple groups are available for approval and if i reject anyone . In sysapproval_group table its approval state is updated to rejected . Next if i approve any other group the rejected approval group is also updating to approved instead of being rejected.

 

 

In attached ss the first group approval is rejected and after approving the next group the first group also got updated to approved. Let me know if there is any oob solution for this scenario. Any help is appreciated , Thanks!

1 ACCEPTED SOLUTION

Abhay Kumar1
Giga Sage

@servicenow14710 In ServiceNow, the behavior you're describing occurs when you have multiple group approvals (using the sysapproval_group table) for a single approval request. When one group rejects the approval, its status is set to "rejected". However, when another group approves it, the entire approval request's status might change to "approved", affecting all related group approvals, including the previously rejected one.

This issue often arises due to the default logic in ServiceNow's approval engine, where the overall approval state is determined based on the most recent action. The default behavior does not account for individual group-level states if the overall request is updated.

As a solution consider using parallel approvals and customizing the rejection logic to halt the process if any group rejects.

Implement a Business Rule to enforce consistent approval states based on initial rejection.

Hope this will help you.

View solution in original post

4 REPLIES 4

Abhay Kumar1
Giga Sage

@servicenow14710 In ServiceNow, the behavior you're describing occurs when you have multiple group approvals (using the sysapproval_group table) for a single approval request. When one group rejects the approval, its status is set to "rejected". However, when another group approves it, the entire approval request's status might change to "approved", affecting all related group approvals, including the previously rejected one.

This issue often arises due to the default logic in ServiceNow's approval engine, where the overall approval state is determined based on the most recent action. The default behavior does not account for individual group-level states if the overall request is updated.

As a solution consider using parallel approvals and customizing the rejection logic to halt the process if any group rejects.

Implement a Business Rule to enforce consistent approval states based on initial rejection.

Hope this will help you.

Hello @Abhay Kumar1: Thanks for your reply, these group approvals are parallel even then im facing this issue.

i used condition based script :

workflow.scratchpad.totalCount = 0;
workflow.scratchpad.totalRejectedCount = 0;
for (var id in groups) { //used to iterate the each group

    var group = groups[id];

    if (group.total >= 1) { // checking approvers count for each group      

        if (group.approved == 1) { //checking the condition for one approver

            workflow.scratchpad.approvedGroupId.push(id);
            fncSetGroupState(id, 'approved'); //calling the function
        } else if (group.rejected == 1) {

            workflow.scratchpad.totalRejectedCount = workflow.scratchpad.totalRejectedCount + 1;
            workflow.scratchpad.rejectedGroupId.push(id);
            fncSetGroupState(id, 'rejected'); //calling the function
        }
    }
}

//the below function is used to update the group approval record
function fncSetGroupState(sidGroupApproval, state) {
    workflow.scratchpad.totalCount = workflow.scratchpad.totalCount + 1;

    var objGroupApproved = new GlideRecord('sysapproval_group');
    objGroupApproved.get(sidGroupApproval);
    objGroupApproved.approval = state;
    objGroupApproved.setWorkflow(false);
    objGroupApproved.update(); //updating the group approval state
    fnSetAppState(id, "not_required");

}

//to update the state of relevent Grp_User approvals
function fnSetAppState(gid, state) {

    var approval = new GlideRecord('sysapproval_approver');
    approval.addQuery('u_reqitem', current.sys_id);
    approval.addQuery('group', gid);
    approval.addQuery('state', 'requested');
    approval.query();
    while (approval.next()) {

        approval.state = state;
        approval.setWorkflow(false);
        approval.update();
    }
}

//all groups rejected, over all request rejected, else proceed further

if (workflow.scratchpad.totalCount == counts.total) { //if none of the group approval is not in requested state

    if (workflow.scratchpad.totalRejectedCount == counts.total) {

        answer = 'rejected';
    } else if (workflow.scratchpad.totalRejectedCount < counts.total) {

        answer = 'approved';
    }
}

@servicenow14710 Setting answer to 'approved' just because not all groups rejected might be incorrect. Typically, you need to check if all groups have approved, not just that they haven't rejected.

You might want to include an 'in progress' or 'pending' state check in case not all approvals are completed.

For example:

if (workflow.scratchpad.totalCount == counts.total) {

    if (workflow.scratchpad.totalRejectedCount == counts.total) {

        // All groups rejected

        answer = 'rejected';

    } else if (workflow.scratchpad.totalApprovedCount == counts.total) {

        // All groups approved

        answer = 'approved';

    } else {

        // Some groups have not decided yet (pending state)

        answer = 'pending';

    }

}

Also add log to debug your code if still result is not expected from your script

Hello @Abhay Kumar1 , thanks for your suggestion. In our scenario even if one approved it should go to approved and all should be rejected to go to rejected

This script i used is for overall approval activity approved/rejected, group approval records are getting updated meanwhile. Thanks!