Skip to content

Commit ab27c84

Browse files
authored
Count open incidents group by Priority and State using GlideAggregate (#2613)
* Initial create script.js * Create README.md * Updated mistyped variable
1 parent af47e27 commit ab27c84

File tree

2 files changed

+74
-0
lines changed
  • Core ServiceNow APIs/GlideAggregate/Count open Incidents per Priority and State using GlideAggregate

2 files changed

+74
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Count open Incidents per Priority and State using GlideAggregate
2+
3+
## Overview
4+
This script will dynamically calculate the **number of open incidents** for each priority level and also give you a total for what
5+
current state the Incident is in using **server-side scripting**
6+
Priority levels typically include:
7+
+ 1 – Critical
8+
+ 2 – High
9+
+ 3 – Moderate
10+
+ 4 – Low
11+
12+
Incident State typically include:
13+
+ New
14+
+ In Progress
15+
+ On Hold
16+
+ Resolved
17+
+ Closed
18+
+ Canceled
19+
20+
The scripting solution leverages **GlideAggregate** to efficiently count records grouped by priority and state. This scripts approach
21+
is useful for:
22+
+ Dashboards
23+
+ Business Rules
24+
+ SLA monitoring and reporting
25+
26+
--
27+
## Table and Fields
28+
+ **Table:** Task
29+
+ **Fields:** Priority, State
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
*Going to define the Incident Closed and Canceled state since we dont want those records as part of our query.
3+
*Also going to leverage the IncidentStateSNC script from ServiceNow
4+
*/
5+
6+
/*
7+
*Going to define the Incident Closed and Canceled state since we dont want those records as part of our query.
8+
*/
9+
var incident_close = IncidentStateSNC.CLOSED;
10+
var incident_canceled = IncidentStateSNC.CANCELED;
11+
var incident_state_query = incident_close + "," + incident_canceled;
12+
13+
/*
14+
*Creating the Incident State value object that will house the correct incident state since we are working from the Task table.
15+
*Leveraging the IncidentStateSNC script from ServiceNow to get the values that they should be
16+
*/
17+
var incident_states = {
18+
'1':'New',
19+
'2':'In Progress',
20+
'3':'On Hold',
21+
'6':'Resolved',
22+
'7':'Closed',
23+
'8':'Canceled'
24+
};
25+
26+
//Going to create the GlideAggregate object
27+
var ga = new GlideAggregate('task');
28+
ga.addQuery('state', 'NOT IN', incident_state_query); //Going to exclude the canceled and closed incidents
29+
ga.addQuery('sys_class_name', 'incident'); //Since working on the Task table need to grab only Incident records with task type
30+
ga.groupBy('state');
31+
ga.groubBy('count');
32+
ga.addAggregate('COUNT');
33+
ga.query()
34+
35+
gs.info('The following is a list of Open Incident records');
36+
37+
while (ga.next()) {
38+
39+
var priorityValue = ga.getDisplayValue('priority');
40+
var state = ga.getValue('state');
41+
var count = ga.getValue('COUNT');
42+
43+
gs.info("There are a total of: " + count + " Incidents with a priority of " + priorityValue + " and in a state of " + incident_states[state]);
44+
45+
}

0 commit comments

Comments
 (0)