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/6] 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/6] 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); +} From a63a20a4c545a8fb00ba39feb8fc6df5ea5309d7 Mon Sep 17 00:00:00 2001 From: Soumyadeep Dutta <78016846+Soumyadeep10@users.noreply.github.com> Date: Tue, 14 Oct 2025 14:40:21 +0530 Subject: [PATCH 3/6] Create README.md --- .../README.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/README.md diff --git a/Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/README.md b/Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/README.md new file mode 100644 index 0000000000..65693b6c3b --- /dev/null +++ b/Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/README.md @@ -0,0 +1,23 @@ +**Usecase**: +This piece of code can be used to check whether a record was created more than 90 days ago or not. +It returns an output of true/false as well as the actual age (in days) of the record. + +**Type**: +Background Script + +**How it works**: +There is a pastDateString variable which can contain a date-time which you received by querying any particular record's sys_created_on field or any other relevant field. +In the actual code I have hard-coded a value to be used as an example. +-There are two gliderecord objects to store the current date as well as past date +-GlideDateTime.subtract() is used to calculate the time difference as a GlideDuration object +-The time duration in milliseconds is converted to days by dividing with 86400000(milliseconds in a day) +-Comparison is done between the results +-The final age of the record and true/false result of the comparison is the output. +[Note: Output may vary according to timezones] + +**Example**: +pastDateString = 2025-05-01 10:00:00 +Output: + + Record Age (Days): 165 + Older than 90 days? true From 8cbb8850cf517b7cf519ccf8c7cd394460840595 Mon Sep 17 00:00:00 2001 From: Soumyadeep Dutta <78016846+Soumyadeep10@users.noreply.github.com> Date: Tue, 14 Oct 2025 14:40:54 +0530 Subject: [PATCH 4/6] Create checkdateover90.js --- .../checkdateover90.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/checkdateover90.js diff --git a/Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/checkdateover90.js b/Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/checkdateover90.js new file mode 100644 index 0000000000..958f10069c --- /dev/null +++ b/Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/checkdateover90.js @@ -0,0 +1,20 @@ +//the pastDateString variable can contain a date-time which you received by querying any particular record's sys_created_on field or any other relevant field. Here I'm hard-coding a value. + +var pastDateString = '2025-05-01 10:00:00'; //input date and time +var ageThresholdDays = 90; + +var gdtPast = new GlideDateTime(pastDateString); +var gdtNow = new GlideDateTime(); +var duration = GlideDateTime.subtract(gdtPast, gdtNow); +var durationMs = duration.getNumericValue(); + +//Calculate the total days (1 day = 86,400,000 milliseconds) +var totalDaysOld = Math.floor(durationMs / 86400000); + +var isOlderThanThreshold = false; +if (totalDaysOld > ageThresholdDays) { + isOlderThanThreshold = true; +} + +gs.info("Record Age (Days): " + totalDaysOld); +gs.info("Older than " + ageThresholdDays + " days? " + isOlderThanThreshold); From fee2c4fa5c207dd5a850d08efb80fbc3bc8cbd19 Mon Sep 17 00:00:00 2001 From: Soumyadeep Dutta <78016846+Soumyadeep10@users.noreply.github.com> Date: Tue, 14 Oct 2025 17:23:17 +0530 Subject: [PATCH 5/6] README.md --- .../Check Record Creation over 90 days and output age/README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Core ServiceNow APIs/GlideDateTime => Server-Side Components/Background Scripts}/Check Record Creation over 90 days and output age/README.md (100%) diff --git a/Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/README.md b/Server-Side Components/Background Scripts/Check Record Creation over 90 days and output age/README.md similarity index 100% rename from Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/README.md rename to Server-Side Components/Background Scripts/Check Record Creation over 90 days and output age/README.md From e9aa045d5f47dd883aa5f69b2445cbd5ae36b2df Mon Sep 17 00:00:00 2001 From: Soumyadeep Dutta <78016846+Soumyadeep10@users.noreply.github.com> Date: Tue, 14 Oct 2025 17:24:37 +0530 Subject: [PATCH 6/6] checkdateover90.js --- .../checkdateover90.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Core ServiceNow APIs/GlideDateTime => Server-Side Components/Background Scripts}/Check Record Creation over 90 days and output age/checkdateover90.js (100%) diff --git a/Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/checkdateover90.js b/Server-Side Components/Background Scripts/Check Record Creation over 90 days and output age/checkdateover90.js similarity index 100% rename from Core ServiceNow APIs/GlideDateTime/Check Record Creation over 90 days and output age/checkdateover90.js rename to Server-Side Components/Background Scripts/Check Record Creation over 90 days and output age/checkdateover90.js