- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2025 01:10 AM
I have to write scheduled job script for specific domain. need to remove roles and groups from inactive users not log in from 180days
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2025 02:27 AM
Hi Amitbodkhe, I recommend writing your request as a question and preferably in a way that shows you've tried to solve the issue somehow yourself. This basically just sounds like you're ordering a piece of script rather than asking for help in making one.
Anyway,
You'll want to query the sys_user_grmember table.
Go to the table and use the filter to create a filter that gives you the list of people that have been inactive for over 180days. Then you can use that query condition in your <gliderecord>.addQuery("<condition here>"); when making your query.
Then to delete them you can either use <gliderecord>.deleteRecord() or <gliderecord>.deleteMultiple() commands to delete them. I recommend reading documentation on how to use both so that you don't accidentally delete all roles and memberships from all users.
Also you can also do the same for sys_user_has_role table to just make sure that if there were any roles that were directly added to a user. Most should be deleted though when you delete the group membership.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2025 02:39 AM
Hi @Weird ,
Due to interwork issue, i was unable to write complete statement. Somehow i was asking for help only.
Sorry inconvenience.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2025 02:27 AM
Hi Amitbodkhe, I recommend writing your request as a question and preferably in a way that shows you've tried to solve the issue somehow yourself. This basically just sounds like you're ordering a piece of script rather than asking for help in making one.
Anyway,
You'll want to query the sys_user_grmember table.
Go to the table and use the filter to create a filter that gives you the list of people that have been inactive for over 180days. Then you can use that query condition in your <gliderecord>.addQuery("<condition here>"); when making your query.
Then to delete them you can either use <gliderecord>.deleteRecord() or <gliderecord>.deleteMultiple() commands to delete them. I recommend reading documentation on how to use both so that you don't accidentally delete all roles and memberships from all users.
Also you can also do the same for sys_user_has_role table to just make sure that if there were any roles that were directly added to a user. Most should be deleted though when you delete the group membership.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2025 02:39 AM
Hi @Weird ,
Due to interwork issue, i was unable to write complete statement. Somehow i was asking for help only.
Sorry inconvenience.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2025 03:00 AM
so please share what script did you try so far and what's not working?
what debugging did you perform?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2025 11:33 PM
HI @Ankur Bawiskar ,
var usr = new GlideRecord('sys_user');
var rlo = new GlideRecord('sys_user_grmember');
usr.addQuery('active', false);
usr.query();
while (usr.next()) {
rlo.addQuery('user', usr.sys_id);
rlo.query();
while (rlo.next()) {
usr.deleteMultiple();
}
}
i have write this script now i want to add domain Please suggest.