Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Use-case:
**Fetch Top 5 CIs with the most number of Open Incidents along with the count**

Type of Script writted: **Background Script**

**How the code works:**
The code uses the GlideAggregate API to efficiently calculate and retrieve the results -
1. A GlideAggregate query is initiated on the Incident table. The query is restricted to only active Incidents.
2. The query instructs the database to COUNT records grouped by the configuration item(cmdb_ci).
3. Furthermore, the records are instructed to be in descending order of number of incidents related to one CI, also a limit
of 5 records are applied to be fetched.
4. The query is executed and a loop is iterated over these 5 records to fetch and print
the CI name and its corresponding incident count.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var countOfCI = 5;
var inc = new GlideAggregate('incident');
inc.addActiveQuery();
inc.addAggregate('COUNT', 'cmdb_ci');
inc.groupBy('cmdb_ci');
inc.orderByAggregate('COUNT', 'cmdb_ci');
inc.setLimit(countOfCI);
inc.query();
gs.info('---Top ' + countOfCI + ' CIs with Most Open Incidents---');


while (inc.next()) {
var ciName;
var ciSysID = inc.cmdb_ci;
var count = inc.getAggregate('COUNT', 'cmdb_ci');
var ci = new GlideRecord('cmdb_ci');
if (ci.get(ciSysID)) { //retrieving the CI record
ciName = ci.name.toString();
} else {
ciName = 'Invalid CI';
}

gs.info('. CI: ' + ciName + ' | Count of Inc: ' + count);
}
Loading