The CreatorCon Call for Content is officially open! Get started here.

Only show assignment group if one or more members are active users

mattmm
Kilo Sage

HI All, I've had an issue where my Change Model is skipping a peer approval step because some of Peer approver groups have all inactive users (I have a "Peer approval group" field that references the group table).

 

How do I do 1 of the following, and what is best practice 

1. Deactivate any Group in the group table that also contains all inactive users (every user is inactive in that group) and then how do I ensure those groups do not then show in the assignment group field, and my "Peer approval group" field?

2. Not show any group that has inactive users as a choice in my "Peer approval group" field?

1 ACCEPTED SOLUTION

Hello @mattmm ,

 

Please try below script, it should fulfill your requirement.

var groupGR = new GlideRecord('sys_user_group');
//groupGR.addQuery('sys_id','12a586cd0bb23200ecfd818393673a30');
groupGR.query();
while (groupGR.next()) {
    var hasActiveUsers = false;
    var userGR = new GlideRecord('sys_user_grmember');
    userGR.addQuery('group.sys_id',groupGR.sys_id);
    userGR.query();
   if(userGR.hasNext()){
     while(userGR.next()){
        //gs.info("Inside IF "+ userGR.user.user_name+"  "+ userGR.group.name);
        if (userGR.user.active+'' == 'true') {
            hasActiveUsers = true;
            break;  // Exit the loop as soon as an active user is found
        }
     }
     if(hasActiveUsers == false){
     gs.info('Marked group as inactive: ' + groupGR.name);
     groupGR.active = 'false';
     groupGR.update();
     }
   } else {
        gs.info('Marked group as inactive: ' + groupGR.name);
        groupGR.active = 'false';
        groupGR.update();
   }
}



 

This script will give you all the groups that has either all inactive users or no users at all. I have tried and tested it in my PDI.

 

Please mark my answer(s) as Accepted✔️ and Helpful👍!

 

View solution in original post

6 REPLIES 6

Thanks so much @Vrushali Kolte  this worked to an extent, but I also need this to apply for any Groups that have either empty group members (i.e. no users within the group) and empty nested groups (only if the users are also empty). I noticed any of these groups don't exist on the sys_user_grmember table if they have no group members. So would it be something like a adding a query to compare if a group existed in the group table, but not in group member table, then apply the same result?

I'm not sure if this is right, or where to put it in the script you've provided, but in the interests of trying to do this myself is something like this ok ?

 

 

 

var group= new GlideRecord('sys_user_group');
group.query();

while (group.next()) {
   gr.addQuery("group", group.sys_id.toString());
   gr.query();
    if (gr.getRowCount() == 0) {
        gr.active = false;
gr.update();
    }
}

 

 

 

I'm just not sure where I would add this within your script, or if it is even right? Hoping you can help me here also?

Hello @mattmm ,

 

Please try below script, it should fulfill your requirement.

var groupGR = new GlideRecord('sys_user_group');
//groupGR.addQuery('sys_id','12a586cd0bb23200ecfd818393673a30');
groupGR.query();
while (groupGR.next()) {
    var hasActiveUsers = false;
    var userGR = new GlideRecord('sys_user_grmember');
    userGR.addQuery('group.sys_id',groupGR.sys_id);
    userGR.query();
   if(userGR.hasNext()){
     while(userGR.next()){
        //gs.info("Inside IF "+ userGR.user.user_name+"  "+ userGR.group.name);
        if (userGR.user.active+'' == 'true') {
            hasActiveUsers = true;
            break;  // Exit the loop as soon as an active user is found
        }
     }
     if(hasActiveUsers == false){
     gs.info('Marked group as inactive: ' + groupGR.name);
     groupGR.active = 'false';
     groupGR.update();
     }
   } else {
        gs.info('Marked group as inactive: ' + groupGR.name);
        groupGR.active = 'false';
        groupGR.update();
   }
}



 

This script will give you all the groups that has either all inactive users or no users at all. I have tried and tested it in my PDI.

 

Please mark my answer(s) as Accepted✔️ and Helpful👍!