From 7547794603be07d9e5f1e975c6097dba125e43ce Mon Sep 17 00:00:00 2001 From: Soumyadeep Dutta <78016846+Soumyadeep10@users.noreply.github.com> Date: Mon, 13 Oct 2025 17:48:00 +0530 Subject: [PATCH 1/2] Create README.md --- .../README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Core ServiceNow APIs/GlideAggregate/Get top 5 CIs with most number of Open Incidents/README.md diff --git a/Core ServiceNow APIs/GlideAggregate/Get top 5 CIs with most number of Open Incidents/README.md b/Core ServiceNow APIs/GlideAggregate/Get top 5 CIs with most number of Open Incidents/README.md new file mode 100644 index 0000000000..c3012906c7 --- /dev/null +++ b/Core ServiceNow APIs/GlideAggregate/Get top 5 CIs with most number of Open Incidents/README.md @@ -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. From 5ed384184e05ab25632d545f4853c757393dedec Mon Sep 17 00:00:00 2001 From: Soumyadeep Dutta <78016846+Soumyadeep10@users.noreply.github.com> Date: Mon, 13 Oct 2025 17:49:25 +0530 Subject: [PATCH 2/2] Create getCIwithmostActiveInc.js --- .../getCIwithmostActiveInc.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Core ServiceNow APIs/GlideAggregate/Get top 5 CIs with most number of Open Incidents/getCIwithmostActiveInc.js diff --git a/Core ServiceNow APIs/GlideAggregate/Get top 5 CIs with most number of Open Incidents/getCIwithmostActiveInc.js b/Core ServiceNow APIs/GlideAggregate/Get top 5 CIs with most number of Open Incidents/getCIwithmostActiveInc.js new file mode 100644 index 0000000000..2abf6fdb8b --- /dev/null +++ b/Core ServiceNow APIs/GlideAggregate/Get top 5 CIs with most number of Open Incidents/getCIwithmostActiveInc.js @@ -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); +}