Skip to content

Commit a6e23bd

Browse files
README.md
1 parent fa78c36 commit a6e23bd

File tree

1 file changed

+46
-0
lines changed
  • Core ServiceNow APIs/GlideAggregate/Count Inactive Users with Active incidents

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Count Active Incidents Assigned to Inactive Users
2+
This script uses GlideAggregate to efficiently count the number of active incidents assigned to inactive users. This is a crucial task for maintaining data hygiene and preventing incidents from being stalled due to inactive assignees.
3+
Overview
4+
The script performs the following actions:
5+
Initializes GlideAggregate: Creates an aggregate query on the incident table.
6+
Filters Records: Uses addQuery() to restrict the search to incidents that are both active (true) and assigned to a user whose active status is false. This filter uses a "dot-walk" on the assigned_to field to check the user's active status directly within the query.
7+
Aggregates by Count: Uses addAggregate() to count the number of incidents, grouping the results by assigned_to user.
8+
Executes and Logs: Runs the query, then loops through the results. For each inactive user found, it logs their name and the number of active incidents assigned to them.
9+
Use case
10+
This script is essential for regular cleanup and maintenance. It can be used in:
11+
Scheduled Job: Automatically run the script daily or weekly to monitor for and report on incidents assigned to inactive users.
12+
Fix Script: Perform a one-off run to identify and log all current incidents that need reassignment.
13+
Reporting: The output from this script can be used to build a report for managers to track and reassign incidents promptly.
14+
Script
15+
javascript
16+
var ga = new GlideAggregate('incident');
17+
ga.addQuery('active', true);
18+
ga.addQuery('assigned_to.active', false);
19+
ga.addAggregate('COUNT', 'assigned_to');
20+
ga.query();
21+
22+
while (ga.next()) {
23+
var inactiveUser = ga.assigned_to.getDisplayValue();
24+
var taskCount = ga.getAggregate('COUNT', 'assigned_to');
25+
gs.info("Inactive user " + inactiveUser + " has " + taskCount + " active incidents.");
26+
}
27+
Use code with caution.
28+
29+
Installation
30+
As a Scheduled Job
31+
Navigate to System Definition > Scheduled Jobs.
32+
Click New and select Automatically run a script of your choosing.
33+
Name the job (e.g., Find Incidents Assigned to Inactive Users).
34+
Set your desired frequency and time.
35+
Paste the script into the Run this script field.
36+
Save and activate the job.
37+
As a Fix Script
38+
Navigate to System Definition > Fix Scripts.
39+
Click New.
40+
Name it (e.g., Find Active Incidents with Inactive Assignee).
41+
Paste the script into the Script field.
42+
Run the script to see the results in the System Log.
43+
Customization
44+
Targeted tables: Change the table name from incident to task or any other table with an assigned_to field to check for active records assigned to inactive users.
45+
Automated reassignment: Extend the script to automatically reassign the incidents to a group or another user. This is a common practice to ensure that tickets do not get stuck in the queue.
46+
Email notification: Instead of logging the information, modify the script to send an email notification to the group manager or another stakeholder with the list of incidents needing attention.

0 commit comments

Comments
 (0)