|
| 1 | +ServiceNow Script: Find Oldest Open Incidents per Group |
| 2 | +This script leverages GlideAggregate to efficiently find the oldest active incident for each assignment group. This is a powerful tool for monitoring and reporting on potential service level agreement (SLA) risks and improving incident management processes. |
| 3 | +Overview |
| 4 | +The script performs the following actions: |
| 5 | +Initializes GlideAggregate: Creates an aggregate query on the incident table. |
| 6 | +Filters Active Incidents: Uses addActiveQuery() to restrict the search to only open (active) incidents. |
| 7 | +Aggregates by Minimum Date: Finds the minimum (MIN) opened_at date, which represents the oldest record. |
| 8 | +Groups by Assignment Group: Groups the results by the assignment_group to get a separate result for each team. |
| 9 | +Iterates and Logs: Loops through the query results and logs the assignment group and the opening date of its oldest open incident. |
| 10 | +How to use |
| 11 | +This script is intended to be used in a server-side context within a ServiceNow instance. Common use cases include: |
| 12 | +Scheduled Job: Run this script on a regular schedule (e.g., daily) to generate a report on aging incidents. |
| 13 | +Script Include: Incorporate the logic into a reusable function within a Script Include, allowing other scripts to call it. |
| 14 | + |
| 15 | +Use code with caution. |
| 16 | + |
| 17 | +Installation |
| 18 | +As a Scheduled Job |
| 19 | +Navigate to System Definition > Scheduled Jobs. |
| 20 | +Click New and select Automatically run a script of your choosing. |
| 21 | +Name the job (e.g., Find Oldest Open Incidents). |
| 22 | +Set your desired frequency and time. |
| 23 | +Paste the script into the Run this script field. |
| 24 | +Save and activate the job. |
| 25 | +As a Script Include |
| 26 | +Navigate to System Definition > Script Includes. |
| 27 | +Click New. |
| 28 | +Name it (e.g., IncidentHelper). |
| 29 | +API Name: global.IncidentHelper |
| 30 | + |
| 31 | + |
| 32 | +Customization |
| 33 | +Change the output: Modify the gs.info() line to instead write to a custom log, send an email, or create a report. |
| 34 | +Refine the query: Add more addQuery() statements to filter incidents by other criteria, such as priority or category. |
| 35 | +Change the aggregate: Use MAX instead of MIN to find the newest incident in each group. |
| 36 | +Get incident details: To get the actual incident record (e.g., its number), you would need to perform a secondary GlideRecord query based on the aggregated data. |
| 37 | +Dependencies |
| 38 | +This script uses standard ServiceNow APIs (GlideAggregate, gs). No external libraries are required. |
0 commit comments