Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

How to Automatically Deactivate Users Who Haven’t Logged in for over 90 Days

KarangulaB
Tera Contributor

I have a requirement to automatically deactivate users in ServiceNow who haven’t logged in for over 90 days. We want to run a check daily.

Scenario : 

Scheduled Business Rule that runs daily, checking the sys_user table for users who haven’t logged in for the specified period and updates their active status.

 

1 ACCEPTED SOLUTION

SumanthDosapati
Mega Sage

@KarangulaB 

> Navigate to 'Scheduled Jobs' from left navigator under system definition menu

> Create New

> Automatically run a script of your choosing

> Set schedule to run daily at some time

> Add below script

    var daysInactive = 90;
    var cutoffDate = new GlideDateTime();
    cutoffDate.addDaysUTC(-daysInactive);

    gs.info('Starting User Deactivation Job for users inactive since: ' + cutoffDate.getDisplayValue());

    var userGR = new GlideRecord('sys_user');
    userGR.addQuery('active', true); // Only active users
    userGR.addQuery('last_login', '<=', cutoffDate); // Users who haven't logged in for 90+ days
    userGR.query();

    var count = 0;
    while (userGR.next()) {
        userGR.active = false;
        userGR.update();
        count++;
    }
    gs.info('User Deactivation Job completed. Total users deactivated: ' + count);

Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth

View solution in original post

4 REPLIES 4

SumanthDosapati
Mega Sage

@KarangulaB 

> Navigate to 'Scheduled Jobs' from left navigator under system definition menu

> Create New

> Automatically run a script of your choosing

> Set schedule to run daily at some time

> Add below script

    var daysInactive = 90;
    var cutoffDate = new GlideDateTime();
    cutoffDate.addDaysUTC(-daysInactive);

    gs.info('Starting User Deactivation Job for users inactive since: ' + cutoffDate.getDisplayValue());

    var userGR = new GlideRecord('sys_user');
    userGR.addQuery('active', true); // Only active users
    userGR.addQuery('last_login', '<=', cutoffDate); // Users who haven't logged in for 90+ days
    userGR.query();

    var count = 0;
    while (userGR.next()) {
        userGR.active = false;
        userGR.update();
        count++;
    }
    gs.info('User Deactivation Job completed. Total users deactivated: ' + count);

Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth

@KarangulaB 

Did the above solution work? If yes, Accept the solution and mark as helpful to close the thread and benefit other readers.

Regards,
Sumanth

Thanks a lot , it worked! By Execute now it is working , but with time setting it is not updating, don't know why. i have given daily with 00 02 00 this time, i have waited for three days it is not updating.

Check for scheduled job execution history

Go to table "syslog_transaction" and filter for Type "Scheduler". In column "URL" you find the name of the Job.