Deactivate large set of users

Julia Howells
Giga Guru

I have a list of active users from Workday. The count of total employees is inaccurate in ServiceNow. I need to find any employees that are not on my active list and deactivate them in ServiceNow.

One caveat is that I need this to NOT deactivate any Contacts that are not on my list as well. We have 1,897 contacts and 20,551 active employees. 

ServiceNow active user count is 27,158.

With contacts and active employees, our count should be 22,448 (ish) so we have just about 4,710 users that need to be deactivated. Is there a clean way to run this?

1 ACCEPTED SOLUTION

You can use condition for the users, lets say hypothetically you come to decide, all users which are active and first name is Dwight, condition would be

addEncodedQuery("active=true^first_name=Dwight");

 

Or by scripting you can fetch the list of sys_ids in an array and set in encoded query,

addEncodedQuery("sys_idIN"+userArray);// assuming userArray has list of sys_ids you want to deactivate

Best Regards
Aman Kumar

View solution in original post

7 REPLIES 7

Muralidharan BS
Mega Sage
Mega Sage

Hi Julia,

Do you have the list of employee numbers or user-id for those 4710 users? If so then you can run a background script to deactivate it. 

var grSysUser = new GlideRecord('sys_user');
grSysUser.addEncodedQuery("active=true^user_nameINanalytics_viewer,analytics_admin,");
grSysUser.query();
while (grSysUser.next()) {
grSysUser.active = 0;
grSysUser.update();
}
the encoded query in the second line should include the list of all users_name or emp id, tweak the 2nd line accordingly. 
 
Alternatively, you can use a transform map also but the above is safer because you can roll back the entire action if you need to blackout it. 
 

thanks

Musab Rasheed
Tera Sage
Tera Sage

Hello Julia,

Use a fix script and run in background so that you can do other operation, Please find sample script. Mark my answer as correct if that helps.

var count = 0;
var gr = new GlideRecord('sys_user');
gr.addQuery("add condition here");
gr.query();
while (gr.next()) {
count++;
gr.active = false;
gr.setWorkflow(false);
gr.update();
}
gs.print("Total number of deactivated users are: " +count);
Please hit like and mark my response as correct if that helps
Regards,
Musab

Aman Kumar S
Kilo Patron

Hey,

Most efficient way to achieve this would be background script of fix script:

var userGR = new GlideRecord('sys_user');
userGR.addEncodedQuery("add condition here");
userGR.setValue("active", false);
userGR.updateMultiple();

 

Refer to below article to understand how it works:

Use updateMultiple() for Maximum Efficiency!

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions šŸ™‚

Best Regards
Aman Kumar

For the encoded query, I'd need to put all 20,000 plus User IDs?