Scheduled job script need to remove roles and groups from inactive users not log in from 180days

amitbodkhe9
Tera Contributor

I have to write scheduled job script for specific domain. need to remove roles and groups from inactive users not log in from 180days 

2 ACCEPTED SOLUTIONS

Weird
Mega Sage

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.

View solution in original post

amitbodkhe9
Tera Contributor

Hi @Weird ,
Due to interwork issue, i was unable to write complete statement. Somehow i was asking for help only.
Sorry inconvenience.

View solution in original post

5 REPLIES 5

Weird
Mega Sage

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.

amitbodkhe9
Tera Contributor

Hi @Weird ,
Due to interwork issue, i was unable to write complete statement. Somehow i was asking for help only.
Sorry inconvenience.

Ankur Bawiskar
Tera Patron
Tera Patron

@amitbodkhe9 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

amitbodkhe9
Tera Contributor

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.