Scheduled Job Remove user(s) from group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2024 02:43 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2024 02:48 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2024 06:50 AM
Hello @Abhishek_Thakur
Thank you so much for your fast reply. I created it a bit different cause yours didn't worked for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2024 02:55 AM - edited ‎10-23-2024 02:56 AM
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.