Skip to content

Commit 3acdabb

Browse files
Count Inactive Users with Active incidents (#2616)
* script.js Automatically create a problem record from incident volume Use case: Automatically create a problem record if a specific Configuration Item (CI) is associated with more than a certain number of incidents within a defined timeframe. Code snippet This code can be placed in a Scheduled Script Execution or an After Insert Business Rule to check new incidents * README.md * Script.js Identify the oldest active incident for each assignment group. This helps managers focus on long-running tickets that may require special attention. * README.md * script.js This example searches a JSON document for all developers listed under the specified path. * README.md * Update README.md * script.js Identify inactive users who still have unresolved incidents. This helps with offboarding processes and ensures incidents aren't left unattended. * README.md * Update script.js * Delete Core ServiceNow APIs/GlideAggregate/Count Inactive Users with Active incidents/README.md * Delete Core ServiceNow APIs/GlideAggregate/Count Inactive Users with Active incidents/script.js * Delete Core ServiceNow APIs/GlideAggregate/Find oldest Incident based Assignment Groups/README.md * Delete Core ServiceNow APIs/GlideAggregate/Find oldest Incident based Assignment Groups/script.js * Delete Core ServiceNow APIs/GlideJsonPath/GlideJsonPath Reader Example/README.md * Delete Core ServiceNow APIs/GlideJsonPath/GlideJsonPath Reader Example/script.js * script.js Count Inactive Users with Active incidents * README.md * Delete Client-Side Components/Client Scripts/Count Inactive Users with Active incidents/README.md * Delete Client-Side Components/Client Scripts/Count Inactive Users with Active incidents/script.js * README.md * script.js * README.md * Create script.js * Delete Server-Side Components/Server Side/Count Inactive Users with Active incidents directory * Delete Core ServiceNow APIs/GlideAggregate/Create Problem based on incident volume/README.md * Delete Core ServiceNow APIs/GlideAggregate/Create Problem based on incident volume/script.js
1 parent ab27c84 commit 3acdabb

File tree

2 files changed

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

2 files changed

+70
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Count Active Incidents Assigned to Inactive Users
2+
3+
This script uses GlideAggregate to efficiently count the number of active incidents assigned to inactive users.
4+
5+
This is a crucial task for maintaining data hygiene and preventing incidents from being stalled due to inactive assignees.
6+
7+
Overview The script performs the following actions: Initializes GlideAggregate: Creates an aggregate query on the incident table.
8+
9+
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.
10+
11+
This filter uses a "dot-walk" on the assigned_to field to check the user's active status directly within the query.
12+
13+
Aggregates by Count: Uses addAggregate() to count the number of incidents, grouping the results by assigned_to user.
14+
15+
Executes and Logs: Runs the query, then loops through the results.
16+
17+
For each inactive user found, it logs their name and the number of active incidents assigned to them. Use case This script is essential for regular cleanup and maintenance.
18+
19+
It can be used in: Scheduled Job: Automatically run the script daily or weekly to monitor for and report on incidents assigned to inactive users.
20+
21+
Installation As a Scheduled Job Navigate to System Definition > Scheduled Jobs.
22+
23+
24+
Click New and select Automatically run a script of your choosing. Name the job (e.g., Find Incidents Assigned to Inactive Users).
25+
26+
27+
Set your desired frequency and time. Paste the script into the Run this script field. Save and activate the job. As a Fix Script Navigate to System Definition > Fix Scripts.
28+
29+
Click New. Name it (e.g., Find Active Incidents with Inactive Assignee).
30+
31+
32+
Paste the script into the Script field. Run the script to see the results in the System Log.
33+
34+
35+
Customization 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.
36+
37+
38+
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. 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.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
This script uses GlideAggregate to find the number of active incidents
3+
that are assigned to users who are currently marked as inactive.
4+
GlideAggregate is used instead of a standard GlideRecord query
5+
because it is more efficient for performing calculations like COUNT
6+
directly in the database.
7+
*/
8+
var ga = new GlideAggregate('incident');
9+
10+
// Query for active incidents.
11+
ga.addQuery('active', true);
12+
13+
// Use dot-walking to query for incidents assigned to inactive users.
14+
ga.addQuery('assigned_to.active', false);
15+
16+
// Add an aggregate function to count the incidents, grouped by the assigned user.
17+
ga.addAggregate('COUNT', 'assigned_to');
18+
19+
// Execute the query.
20+
ga.query();
21+
22+
// Process the results of the aggregate query.
23+
while (ga.next()) {
24+
// Get the display name of the inactive user from the current record.
25+
var inactiveUser = ga.assigned_to.getDisplayValue();
26+
27+
// Get the count of active incidents for this specific user.
28+
var incidentCount = ga.getAggregate('COUNT', 'assigned_to');
29+
30+
// Log the result to the system logs.
31+
gs.info("Inactive user " + inactiveUser + " has " + incidentCount + " active incidents.");
32+
}

0 commit comments

Comments
 (0)