How to remove roles and Groups for Inactive Users(already I have 600 inactive users).How to remove those past records,For future inactive users i have created Business rule

1415
Kilo Contributor

Hi ,

 

I have already old 600 inactive users .How to remove roles and Groups for those inactive users.I tried to create schedule job But it is not running .

 

Can you please suggest me .

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

You could use this in a background script to remove the groups.

var gr = new GlideRecord('sys_user');
var gr2 = new GlideRecord('sys_user_grmember');
gr.addQuery('active', false);
gr.query();
while (gr.next()) {
gr2.addQuery('user', gr.sys_id);
gr2.query();
while (gr2.next()) {
gr.deleteMultiple();
}
}

This would query for an inactive user, if found, query if they are a member of any groups, if found - delete all membership(s), then moves on to the next user. Keep in mind, this could run for a bit so be mindful when you're doing this.

Now if roles were assigned to users as well (which isn't best practice) ...then you can use this same script but then switchout sys_user_grmember with sys_user_has_role and run it again.

Please mark reply as Helpful/Correct, thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

6 REPLIES 6

Allen Andreas
Administrator
Administrator

Hi,

You could use this in a background script to remove the groups.

var gr = new GlideRecord('sys_user');
var gr2 = new GlideRecord('sys_user_grmember');
gr.addQuery('active', false);
gr.query();
while (gr.next()) {
gr2.addQuery('user', gr.sys_id);
gr2.query();
while (gr2.next()) {
gr.deleteMultiple();
}
}

This would query for an inactive user, if found, query if they are a member of any groups, if found - delete all membership(s), then moves on to the next user. Keep in mind, this could run for a bit so be mindful when you're doing this.

Now if roles were assigned to users as well (which isn't best practice) ...then you can use this same script but then switchout sys_user_grmember with sys_user_has_role and run it again.

Please mark reply as Helpful/Correct, thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Anuran Sen
Kilo Contributor

Please advise as how and where the background script can be run in ServiceNow so memberships is removed from inactive users ?

Hello,

Search "background"....in your left-hand navigation within your ServiceNow instance and then use the script I posted above?

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

geoffwoods
Kilo Contributor

Hi,

 

The line 

while (gr2.next()) {
gr.deleteMultiple();
}

will delete all the inactive users from the sys_user table

 

Should be 

gr2.deleteMultiple()

to delete all records the inactive user has in the sys_user_group table, no need for the while loop

or have I misunderstood?