Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Scheduled Job: Deactivate Inactive Users and Notify Managers

Overview
This scheduled job automatically deactivates inactive users based on their last login time and creation date, and sends an email notification to their active manager using the ServiceNow event framework (gs.eventQueue()).

The entire process is divided into three components:
1.Scheduled Job Script — Finds inactive users and fires an event.
2.Event Registration — Registers user.deactivation.notify_manager in the Event Registry.
3.Script Action — Sends an email to the manager dynamically.

1. Scheduled Job Script

Purpose
This script runs on a schedule (e.g., daily or weekly) and:
Finds users who haven’t logged in for a specific number of days.
Checks their account creation date.
Deactivates those users.
Fires an event to notify their manager if the manager is active.

Logic Summary
Calculates a cutoff date (e.g., 90 days of inactivity).
Queries sys_user for users:
Whose last_login_time is older than the cutoff date OR is empty.
Whose sys_created_on is older than the cutoff date.
Who are currently active.
For each matching user:
Finds their manager record.
Checks if the manager is active.
Deactivates the user.

Sends an event with:
parm1: User name
parm2: Manager’s email

2. Event Registration:

Name: user.deactivation.notify_manager
Table: sys_user
Description: “Triggered when a user is deactivated due to inactivity.”

3. Script Action Setup

Name: Notify Manager on User Deactivation
Event name: user.deactivation.notify_manager
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(function() {
var inactiveDays = 90;
var cutoffDate = new GlideDateTime();
cutoffDate.addDaysUTC(-inactiveDays);

var userGR = new GlideRecord('sys_user');
userGR.addActiveQuery(); // Only active users
userGR.addQuery('sys_created_on', '<', cutoffDate); // Old accounts

// Using encoded query: users with last login before cutoff OR never logged in
userGR.addEncodedQuery('last_login_time<' + cutoffDate.getValue() + '^ORlast_login_timeISEMPTY');
userGR.query();

while (userGR.next()) {
var wasActive = userGR.active;
var managerSysId = userGR.manager;

// Deactivate the user
userGR.active = false;
userGR.update();

// Notify only if manager exists and is active
if (wasActive && managerSysId) {
var mgrGR = new GlideRecord('sys_user');
mgrGR.addQuery('sys_id', managerSysId);
mgrGR.addQuery('active', true);
mgrGR.query();

if (mgrGR.next()) {
gs.eventQueue(
'user.deactivation.notify_manager', // Event name
userGR, // Current user record
userGR.name.toString(), // parm1: user's name
mgrGR.email.toString() // parm2: manager's email
);
}
}
}

gs.info('Inactive or never-logged users deactivated; active managers notified.');
})();

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Script Action: Notify Manager on User Deactivation

Overview
This Script Action is triggered when the event user.deactivation.notify_manager is fired by a background or scheduled job.
It dynamically sends an email notification to the manager of the deactivated user using the GlideEmailOutbound API.

Purpose
Automatically inform a user’s manager when their account has been deactivated due to inactivity.
Use event-driven notification — no direct email sending in the scheduled job script.
Keep manager email addresses dynamic, using event parameters (parm1, parm2).

Event and Parameters
The Script Action listens for this event:
Event name: user.deactivation.notify_manager

Explanation
parm1 and parm2 are populated dynamically by the job that fired the event.
parm1 → user’s name
parm2 → manager’s email
GlideEmailOutbound is used to send emails programmatically without needing a Notification record.
The message body is kept simple and readable, but can be formatted in HTML if needed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(function(current, event, parm1, parm2) {
var userName = parm1;
var managerEmail = parm2;

var subject = 'User Deactivated: ' + userName;
var body = 'Hello,\n\n' +
'The user "' + userName + '" has been deactivated due to inactivity.\n\n' +
'Regards,\nSystem Administrator';

// Send the email using GlideEmailOutbound (manual way)
var mail = new GlideEmailOutbound();
mail.setSubject(subject);
mail.setBody(body);
mail.setTo(managerEmail);
mail.send();
})(current, event, parm1, parm2);

Loading