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.

Scheduled Job Remove user(s) from group

JanaT
Tera Contributor

Dear Community.

I created a scheduled job, which removes inactive users from groups.

The table is: sys_user_grmember

Generally it works, the inactive users are found and removed from their groups (user.active is false all records are deleted).

The only problem I have is that my company wants to have the follwoing information in logs: Inactive user: xy has been removed from groups: a,b,c...

In my logs I have this information multiple, because there are inactive user who have multiple groups. How can I fix that problem and make it more optimal.

See the sreenshot below from the scheduled job and the logs.

 

Scheduled Job:

var grmemberGr = new GlideRecord('sys_user_grmember');
grmemberGr.addQuery('user.active', false);  // Query for inactive users
grmemberGr.query();

var groups = [];
var inactiveUserName = '';

// Iterate through each inactive user's group memberships
while (grmemberGr.next()) {
var currentUserName = grmemberGr.getDisplayValue('user');
var groupName = grmemberGr.getDisplayValue('group');

if (inactiveUserName && currentUserName !== inactiveUserName) {
 if (groups.length > 0) {
gs.info("Inactive user: " + inactiveUserName + " has been removed from groups: " + groups.join(', ') + ".");
}
//Reset the groups array for the new user
groups = [];
}

// Update the current user being processed
    inactiveUserName = currentUserName;

//Add the group to the array
groups.push(groupName);

// Delete the group membership record for the current user and group
grmemberGr.deleteRecord();
}

// After the loop, log the groups for the last user
if (groups.length > 0) {
gs.info("Inactive user: " + inactiveUserName + " has been removed from groups: " + groups.join(', ') + ".");
}
 
Screenshot logs:
JanaT_0-1729676381574.png

 

3 REPLIES 3

Abhishek_Thakur
Mega Sage
Mega Sage

Hello @JanaT ,

You can follow the below script that will remove the inactive user from active groups.

var user = new GlideRecord("sys_user");
user.addInactiveQuery();  // Filter the InActive user from the sys_user table.
user.query();
while(user.next()){
var group = new GlideRecord("sys_user_group");
group.addQuery("user",user.getUniqueValue());  // Compare the group in which Inactive user would find.
group.query();
if(group.next()){
	// gs.print("Group sys_id is "+ group.getUniqueValue());
	var groupMember= new GlideRecord("sys_user_grmember");
	groupMember.addQuery("group",group.getUniqueValue());  // Compare the group member in which Inactive user would find.
	groupMember.query();
	if(groupMember.next()){
		gs.print("Group sys_id is "+ group.getUniqueValue());  // Get the sys_id of the group;
		gs.print("Member sys_id is "+ groupMember.getUniqueValue());  // Get the sys_id of the member which is inactive.
		gs.print("Deleted record is "+ groupMember.sys_id);  // Get the sys_id of the record which is going to delete;
		groupMember.deleteRecord();  // Delete the record
		
	}
}
}

 

Please mark my answer as accepted solution and give thumbs up, if it helps you.

Hello @Abhishek_Thakur 

Thank you so much for your fast reply. I created it a bit different cause yours didn't worked for me.

Abhishek_Thakur
Mega Sage
Mega Sage

Hello @JanaT,

If you found my response helpful, could you please accept my solution. So, that it can help other if they will get stuck in same type of scenario.