Remove users from the group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2024 02:25 AM
Hi all,
schedule job to remove the users from the particular groups who not logged in for 60 days in servicenow.
If user is part of some groups and if he hasn’t logged in for 60 days.
Thank you.
Please suggest
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2024 02:28 AM
Hi @Dileep2
You can do via Flow designer, and need to do dot walk to check the last login for a group.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2024 02:30 AM
Why not create a scheduled flow? Set your schedule and look up all sys_user_grmember records with your group names. For each record, find all active users with a last login date/time of over 60 days ago and delete the records.
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2024 02:33 AM
Hi @Dileep2
As mentioned by others, flow is the right way to go where you need not write a code, but if you prefer scheduled job, try below script:
var userGr = new GlideRecord('sys_user');
userGr.addQuery('active', true); // Only active users
userGr.addQuery('last_login', '<', gs.daysAgo(60)); // Users who haven't logged in for 60 days
userGr.query();
while (userGr.next()) {
var user = userGr.getUniqueValue();
var groupMembershipGr = new GlideRecord('sys_user_grmember');
groupMembershipGr.addQuery('user', user);
groupMembershipGr.query();
while(groupMembershipGr.next()) {
groupMembershipGr.deleteRecord();
}
}
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2024 02:42 AM
You can try below code template..Please replace with actual field in the code:
// Define the number of days for the threshold
var daysThreshold = 60;
// Get the current date and time
var currentDate = new GlideDateTime();
currentDate.setDisplayValue(currentDate);
// Calculate the date threshold (60 days ago)
var thresholdDate = new GlideDateTime();
thresholdDate.subtractDaysLocalTime(daysThreshold);
// Query users who haven't logged in for 60 days
var userGr = new GlideRecord('sys_user');
userGr.addQuery('last_login', '<', thresholdDate);
userGr.query();
while (userGr.next()) {
// Get the user's ID
var userId = userGr.sys_id.toString();
// Retrieve group memberships for the user
var groupGr = new GlideRecord('sys_user_grmember');
groupGr.addQuery('user', userId);
groupGr.query();
// Iterate through the user's group memberships
while (groupGr.next()) {
var groupId = groupGr.group.toString();
var groupName = groupGr.group.getDisplayValue(); // Optional: Get group name for logging
// Check if the user is a member of a specific group(s)
// Replace 'GROUP_NAME' with the name of the group you want to target
if (groupName == 'GROUP_NAME') {
// Remove the user from the group
groupGr.deleteRecord();
groupGr.update();
]
}
}
}
Note: Please validate it first on your PDI
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks