Skip to content

Commit dcd5bd4

Browse files
README.md
1 parent e1e3760 commit dcd5bd4

File tree

1 file changed

+90
-0
lines changed
  • Core ServiceNow APIs/GlideAggregate/Find Oldest Open Incidents per Group

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
ServiceNow Script: Find Oldest Open Incidents per Group
2+
This script leverages GlideAggregate to efficiently find the oldest active incident for each assignment group. This is a powerful tool for monitoring and reporting on potential service level agreement (SLA) risks and improving incident management processes.
3+
Overview
4+
The script performs the following actions:
5+
Initializes GlideAggregate: Creates an aggregate query on the incident table.
6+
Filters Active Incidents: Uses addActiveQuery() to restrict the search to only open (active) incidents.
7+
Aggregates by Minimum Date: Finds the minimum (MIN) opened_at date, which represents the oldest record.
8+
Groups by Assignment Group: Groups the results by the assignment_group to get a separate result for each team.
9+
Iterates and Logs: Loops through the query results and logs the assignment group and the opening date of its oldest open incident.
10+
How to use
11+
This script is intended to be used in a server-side context within a ServiceNow instance. Common use cases include:
12+
Scheduled Job: Run this script on a regular schedule (e.g., daily) to generate a report on aging incidents.
13+
Script Include: Incorporate the logic into a reusable function within a Script Include, allowing other scripts to call it.
14+
Fix Script: Execute the script as a one-off task to perform an analysis.
15+
Script
16+
javascript
17+
(function findOldestIncidents() {
18+
var ga = new GlideAggregate('incident');
19+
ga.addActiveQuery();
20+
ga.addAggregate('MIN', 'opened_at');
21+
ga.groupBy('assignment_group');
22+
ga.query();
23+
24+
while (ga.next()) {
25+
var group = ga.assignment_group.getDisplayValue();
26+
var oldestIncidentDate = ga.getAggregate('MIN', 'opened_at');
27+
gs.info("Oldest open incident for " + group + " was created on: " + oldestIncidentDate);
28+
}
29+
})();
30+
Use code with caution.
31+
32+
Installation
33+
As a Scheduled Job
34+
Navigate to System Definition > Scheduled Jobs.
35+
Click New and select Automatically run a script of your choosing.
36+
Name the job (e.g., Find Oldest Open Incidents).
37+
Set your desired frequency and time.
38+
Paste the script into the Run this script field.
39+
Save and activate the job.
40+
As a Script Include
41+
Navigate to System Definition > Script Includes.
42+
Click New.
43+
Name it (e.g., IncidentHelper).
44+
API Name: global.IncidentHelper
45+
Script:
46+
javascript
47+
var IncidentHelper = Class.create();
48+
IncidentHelper.prototype = {
49+
initialize: function() {
50+
},
51+
52+
findOldestIncidents: function() {
53+
var ga = new GlideAggregate('incident');
54+
ga.addActiveQuery();
55+
ga.addAggregate('MIN', 'opened_at');
56+
ga.groupBy('assignment_group');
57+
ga.query();
58+
59+
var results = [];
60+
while (ga.next()) {
61+
var group = ga.assignment_group.getDisplayValue();
62+
var oldestIncidentDate = ga.getAggregate('MIN', 'opened_at');
63+
results.push({
64+
group: group,
65+
oldestIncidentDate: oldestIncidentDate
66+
});
67+
}
68+
return results;
69+
},
70+
71+
type: 'IncidentHelper'
72+
};
73+
Use code with caution.
74+
75+
You can then call this function from other scripts. For example:
76+
javascript
77+
var helper = new IncidentHelper();
78+
var incidents = helper.findOldestIncidents();
79+
for (var i = 0; i < incidents.length; i++) {
80+
gs.info("Oldest open incident for " + incidents[i].group + " was created on: " + incidents[i].oldestIncidentDate);
81+
}
82+
Use code with caution.
83+
84+
Customization
85+
Change the output: Modify the gs.info() line to instead write to a custom log, send an email, or create a report.
86+
Refine the query: Add more addQuery() statements to filter incidents by other criteria, such as priority or category.
87+
Change the aggregate: Use MAX instead of MIN to find the newest incident in each group.
88+
Get incident details: To get the actual incident record (e.g., its number), you would need to perform a secondary GlideRecord query based on the aggregated data.
89+
Dependencies
90+
This script uses standard ServiceNow APIs (GlideAggregate, gs). No external libraries are required.

0 commit comments

Comments
 (0)