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. 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); +}