Remove users from Approval group if they are not assigned active approvals

Swapnil_22
Tera Contributor

Requirement :- If user has no approval records(sysapproval_approver) in last 6 months, remove them from Approval group (if user has no active approval records(state!=requested^ORstate=NULL) then only remove users from the approval group. Users might be assigned to active & Inactive approvals at the same time, but the array should only return users with inactive approvals records.

I've search the Wiki and Community, but haven't found anything that will tell me how to find get these user list. Any help will be much appreciated!

Tried with the below script:-

var userArray = [];
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("group.name", "TestOnly"); //TestOnly is Approval Group
gr.query();
while (gr.next()) {

    userArray.push(gr.user.toString());
}

gs.info("user list: " +userArray.toString());
var userInState = [];
var isRequested = false;

for (var i in userArray){

    var gr1 = new GlideRecord("sysapproval_approver");
    gr1.addQuery("approver", userArray[i]);
    gr1.query();
    while (gr1.next()) {

        if (gr1.state == "requested"){

            var index = userArray.indexOf(userArray[i]);
            gs.info("index: " +index);
            if (index > -1) { // only splice array when item is found
                gs.info("indexinside: " +index);
                userArray.splice(index, 1); // 2nd parameter means remove one item only
                gs.info("user in state array : " +userArray.toString());
            }
            break;
        }
    }

}

gs.info("final user: " +userArray.toString());   // Final Array should contain users which no active approvals (exclude requested state)

4 REPLIES 4

Sruthi17
Kilo Sage

Hi,


The best solution for this use case is to use flow designer.

 

Create a scheduled flow, that runs weekly or monthly. In the flow define the actions to find the users and remove from group

Please accept solution or helpful, if you find it helpful.

 

Hi Sruthi,

Thanks for the Suggestion..

Currently creating a scheduled job as per the requirment that runs on daily basis that will remove users from the approval group.

But the script is not working as expected, not getting the exepcted list of users in the Final userArray.

Hi,


Please avoid scripting. Create a flow, trigger should be scheduled.

When you use flow designer, you can easily achieve this requirement and also you can easily troubleshoot the failure.

mfhaciahmetoglu
Mega Sage

Hello! 

Were you able to find a solution to this problem?

Thanks.