Skip to content

Commit cc7d794

Browse files
authored
Fetch Top 5 CIs with most number of Open Incidents along with the count (#2115)
* Create README.md * Create getCIwithmostActiveInc.js
1 parent e026d5b commit cc7d794

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Use-case:
2+
**Fetch Top 5 CIs with the most number of Open Incidents along with the count**
3+
4+
Type of Script writted: **Background Script**
5+
6+
**How the code works:**
7+
The code uses the GlideAggregate API to efficiently calculate and retrieve the results -
8+
1. A GlideAggregate query is initiated on the Incident table. The query is restricted to only active Incidents.
9+
2. The query instructs the database to COUNT records grouped by the configuration item(cmdb_ci).
10+
3. Furthermore, the records are instructed to be in descending order of number of incidents related to one CI, also a limit
11+
of 5 records are applied to be fetched.
12+
4. The query is executed and a loop is iterated over these 5 records to fetch and print
13+
the CI name and its corresponding incident count.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var countOfCI = 5;
2+
var inc = new GlideAggregate('incident');
3+
inc.addActiveQuery();
4+
inc.addAggregate('COUNT', 'cmdb_ci');
5+
inc.groupBy('cmdb_ci');
6+
inc.orderByAggregate('COUNT', 'cmdb_ci');
7+
inc.setLimit(countOfCI);
8+
inc.query();
9+
gs.info('---Top ' + countOfCI + ' CIs with Most Open Incidents---');
10+
11+
12+
while (inc.next()) {
13+
var ciName;
14+
var ciSysID = inc.cmdb_ci;
15+
var count = inc.getAggregate('COUNT', 'cmdb_ci');
16+
var ci = new GlideRecord('cmdb_ci');
17+
if (ci.get(ciSysID)) { //retrieving the CI record
18+
ciName = ci.name.toString();
19+
} else {
20+
ciName = 'Invalid CI';
21+
}
22+
23+
gs.info('. CI: ' + ciName + ' | Count of Inc: ' + count);
24+
}

0 commit comments

Comments
 (0)