Disable Inactive Users

Rich Platt
Giga Contributor

Would anyone be able to assist me with creating a mechanism to disable users that have not logged in for 30 days? We are not using an external SSO or User Management system. I am guessing that a business rule would be used to accomplish this? Thanks for your help!

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

you can create scheduled job with below script and run it daily

var Days = gs.daysAgoStart(30).split(' ');
var gr = new GlideRecord('sys_user');
gr.addQuery('last_login', 'CONTAINS', Days[0]);
gr.addActiveQuery();
gr.query();
while (gr.next()) {
    gr.active = false;
    gr.update();
}

or

var gr = new GlideRecord('sys_user');
gr.addEncodedQuery('last_login<javascript:gs.beginningOfLast30Days()');
gr.addActiveQuery();
gr.query();
while (gr.next()) {
    gr.active = false;
    gr.update();
}

View solution in original post

4 REPLIES 4

P-Rudenko-SN
ServiceNow Employee
ServiceNow Employee

Hello, I'd suggest creating a scheduled job, that checks the 'last login' field on 'sys_user' table (in case it's populated).

Mike Patel
Tera Sage

you can create scheduled job with below script and run it daily

var Days = gs.daysAgoStart(30).split(' ');
var gr = new GlideRecord('sys_user');
gr.addQuery('last_login', 'CONTAINS', Days[0]);
gr.addActiveQuery();
gr.query();
while (gr.next()) {
    gr.active = false;
    gr.update();
}

or

var gr = new GlideRecord('sys_user');
gr.addEncodedQuery('last_login<javascript:gs.beginningOfLast30Days()');
gr.addActiveQuery();
gr.query();
while (gr.next()) {
    gr.active = false;
    gr.update();
}

Thanks for the assist!

Hi @Mike Patel 

Can you help me ?

1) I need to automate the activation of an inactive user from the user's login. I would like to deactivate an account when the user was unused for 90 days, but when he needs to use it again, I need to activate it without intervention in the administration of the tool. It's possible ?

Do you have an example of this type of event to be triggered and the script I need to create to activate users ? In which module does it have to be implemented?

2) I have a problem managing disabled users. When disabled they no longer receive notifications and emails about approvals and notifications. Being disabled, it is not possible to generate the approval task and the process is interrupted. Can I work around this situation so that they receive some notification (email) or as soon as they become active again, receive approvals and pending notifications?

Thanks for you help.