Skip to content

Commit 81b587d

Browse files
Deactivate Inactive Users and Notify Managers (#2576)
* Create Readme.md Deactivate Inactive Users and Notify Managers * Scheduled job script code snippet Deactivate Inactive Users and Notify Managers * Create Readme.md Notify Manager on User Deactivation * Script action code snippet Notify Manager on User Deactivation
1 parent 6ec1f27 commit 81b587d

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Scheduled Job: Deactivate Inactive Users and Notify Managers
2+
3+
Overview
4+
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()).
5+
6+
The entire process is divided into three components:
7+
1.Scheduled Job Script — Finds inactive users and fires an event.
8+
2.Event Registration — Registers user.deactivation.notify_manager in the Event Registry.
9+
3.Script Action — Sends an email to the manager dynamically.
10+
11+
1. Scheduled Job Script
12+
13+
Purpose
14+
This script runs on a schedule (e.g., daily or weekly) and:
15+
Finds users who haven’t logged in for a specific number of days.
16+
Checks their account creation date.
17+
Deactivates those users.
18+
Fires an event to notify their manager if the manager is active.
19+
20+
Logic Summary
21+
Calculates a cutoff date (e.g., 90 days of inactivity).
22+
Queries sys_user for users:
23+
Whose last_login_time is older than the cutoff date OR is empty.
24+
Whose sys_created_on is older than the cutoff date.
25+
Who are currently active.
26+
For each matching user:
27+
Finds their manager record.
28+
Checks if the manager is active.
29+
Deactivates the user.
30+
31+
Sends an event with:
32+
parm1: User name
33+
parm2: Manager’s email
34+
35+
2. Event Registration:
36+
37+
Name: user.deactivation.notify_manager
38+
Table: sys_user
39+
Description: “Triggered when a user is deactivated due to inactivity.”
40+
41+
3. Script Action Setup
42+
43+
Name: Notify Manager on User Deactivation
44+
Event name: user.deactivation.notify_manager
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
(function() {
2+
var inactiveDays = 90;
3+
var cutoffDate = new GlideDateTime();
4+
cutoffDate.addDaysUTC(-inactiveDays);
5+
6+
var userGR = new GlideRecord('sys_user');
7+
userGR.addActiveQuery(); // Only active users
8+
userGR.addQuery('sys_created_on', '<', cutoffDate); // Old accounts
9+
10+
// Using encoded query: users with last login before cutoff OR never logged in
11+
userGR.addEncodedQuery('last_login_time<' + cutoffDate.getValue() + '^ORlast_login_timeISEMPTY');
12+
userGR.query();
13+
14+
while (userGR.next()) {
15+
var wasActive = userGR.active;
16+
var managerSysId = userGR.manager;
17+
18+
// Deactivate the user
19+
userGR.active = false;
20+
userGR.update();
21+
22+
// Notify only if manager exists and is active
23+
if (wasActive && managerSysId) {
24+
var mgrGR = new GlideRecord('sys_user');
25+
mgrGR.addQuery('sys_id', managerSysId);
26+
mgrGR.addQuery('active', true);
27+
mgrGR.query();
28+
29+
if (mgrGR.next()) {
30+
gs.eventQueue(
31+
'user.deactivation.notify_manager', // Event name
32+
userGR, // Current user record
33+
userGR.name.toString(), // parm1: user's name
34+
mgrGR.email.toString() // parm2: manager's email
35+
);
36+
}
37+
}
38+
}
39+
40+
gs.info('Inactive or never-logged users deactivated; active managers notified.');
41+
})();
42+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Script Action: Notify Manager on User Deactivation
2+
3+
Overview
4+
This Script Action is triggered when the event user.deactivation.notify_manager is fired by a background or scheduled job.
5+
It dynamically sends an email notification to the manager of the deactivated user using the GlideEmailOutbound API.
6+
7+
Purpose
8+
Automatically inform a user’s manager when their account has been deactivated due to inactivity.
9+
Use event-driven notification — no direct email sending in the scheduled job script.
10+
Keep manager email addresses dynamic, using event parameters (parm1, parm2).
11+
12+
Event and Parameters
13+
The Script Action listens for this event:
14+
Event name: user.deactivation.notify_manager
15+
16+
Explanation
17+
parm1 and parm2 are populated dynamically by the job that fired the event.
18+
parm1 → user’s name
19+
parm2 → manager’s email
20+
GlideEmailOutbound is used to send emails programmatically without needing a Notification record.
21+
The message body is kept simple and readable, but can be formatted in HTML if needed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(function(current, event, parm1, parm2) {
2+
var userName = parm1;
3+
var managerEmail = parm2;
4+
5+
var subject = 'User Deactivated: ' + userName;
6+
var body = 'Hello,\n\n' +
7+
'The user "' + userName + '" has been deactivated due to inactivity.\n\n' +
8+
'Regards,\nSystem Administrator';
9+
10+
// Send the email using GlideEmailOutbound (manual way)
11+
var mail = new GlideEmailOutbound();
12+
mail.setSubject(subject);
13+
mail.setBody(body);
14+
mail.setTo(managerEmail);
15+
mail.send();
16+
})(current, event, parm1, parm2);
17+

0 commit comments

Comments
 (0)