From 3c1502cef015b99e47ff69d6b37b795d43735c61 Mon Sep 17 00:00:00 2001 From: shriramthebeast Date: Mon, 27 Oct 2025 22:35:23 +0530 Subject: [PATCH] feat: Add Incident Trend Analyzer snippet for weekly incident comparison in ServiceNow --- .../Trend Analyzer/IncidentTrendAnalyzer.js | 0 .../Trend Analyzer/README.md | 86 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 Server-Side Components/Trend Analyzer/IncidentTrendAnalyzer.js create mode 100644 Server-Side Components/Trend Analyzer/README.md diff --git a/Server-Side Components/Trend Analyzer/IncidentTrendAnalyzer.js b/Server-Side Components/Trend Analyzer/IncidentTrendAnalyzer.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Server-Side Components/Trend Analyzer/README.md b/Server-Side Components/Trend Analyzer/README.md new file mode 100644 index 0000000000..b5b5b052f9 --- /dev/null +++ b/Server-Side Components/Trend Analyzer/README.md @@ -0,0 +1,86 @@ +# Incident Trend Analyzer + +## Overview +Compares incident volume week-over-week to track trends and identify anomalies in incident patterns. + +## What It Does +- Counts incidents created last week +- Counts incidents created this week +- Calculates the difference (increase/decrease) +- Logs the trend analysis result + +## Use Cases +- Monitor incident volume trends +- Identify weeks with unusual spike/drop in incidents +- Track service health over time +- Alert on incident volume anomalies +- Weekly reporting on incident patterns + +## Files +- `IncidentTrendAnalyzer.js` - GlideAggregate-based trend analysis + +## How to Use + +### Option 1: Run as Scheduled Job +1. Go to **System Scheduler > Scheduled Jobs** +2. Create new Scheduled Job +3. Copy code from `IncidentTrendAnalyzer.js` +4. Set to run weekly (e.g., every Monday morning) +5. Check logs for trend results + +### Option 2: Run from Background Script +1. Go to **System Diagnostics > Script Background** +2. Copy and execute the code +3. View results in logs + +### Example Usage +```javascript +// The script automatically: +// 1. Queries incidents from last week +// 2. Queries incidents from this week +// 3. Compares counts +// 4. Logs: "Incident count increased by X compared to last week." +``` + +## Output Examples +``` +"Incident count increased by 15 compared to last week." +"Incident count decreased by 8 compared to last week." +"No change in incident volume week-over-week." +``` + +## Key Features +- Uses `GlideAggregate` for efficient counting +- No heavy querying of individual records +- Date range filtering using ServiceNow helper functions +- Week-over-week comparison logic + +## Requirements +- ServiceNow instance with Incident table +- Access to run Background Scripts or create Scheduled Jobs +- Read access to incident records + +## Performance Notes +- Very efficient - uses aggregation not GlideRecord loops +- Minimal database impact +- Suitable for running on schedule without performance concerns + +## Customization +To track other tables (Change, Problem, etc.): +```javascript +// Change 'incident' to your table name +var lastWeekAgg = new GlideAggregate('change_request'); +var thisWeekAgg = new GlideAggregate('change_request'); +``` + +To track different time periods: +```javascript +// Use other helper functions: +// gs.beginningOfThisMonth(), gs.endOfThisMonth() +// gs.beginningOfThisYear(), gs.endOfThisYear() +// gs.addMonthsUTC(), gs.addDaysUTC() for custom ranges +``` + +## Related ServiceNow APIs +- [GlideAggregate](https://docs.servicenow.com/bundle/sandiego-application-development/page/app-store/dev_apps/concept/c_UsingGlideAggregate.html) - Used for efficient counting +- [GlideSystem Date Functions](https://docs.servicenow.com/bundle/sandiego-application-development/page/app-store/dev_apps/concept/c_SystemDateFunctions.html)