diff --git a/Core ServiceNow APIs/GlideRecord/Archiving Old Incident Records to Improve Performance/readme.md b/Core ServiceNow APIs/GlideRecord/Archiving Old Incident Records to Improve Performance/readme.md new file mode 100644 index 0000000000..6fe761fa84 --- /dev/null +++ b/Core ServiceNow APIs/GlideRecord/Archiving Old Incident Records to Improve Performance/readme.md @@ -0,0 +1,8 @@ +## Purpose +This document explains how to archive old incident records from the `incident` table to an archive table `ar_incident` to improve performance, while preserving historical data for reporting and audit purposes. +## Solution Overview +Use **ServiceNow Archive Rules** to automatically move incidents to an archive table based on specific conditions: +- Incidents that are **closed**. +- Incidents that are **inactive** (`active = false`). +- Incidents that were closed **150 days ago or earlier**. +The records are moved to the archive table `ar_incident`, which preserves all necessary fields for historical reference. diff --git a/Core ServiceNow APIs/GlideRecord/Archiving Old Incident Records to Improve Performance/script.js b/Core ServiceNow APIs/GlideRecord/Archiving Old Incident Records to Improve Performance/script.js new file mode 100644 index 0000000000..f75c926f73 --- /dev/null +++ b/Core ServiceNow APIs/GlideRecord/Archiving Old Incident Records to Improve Performance/script.js @@ -0,0 +1,14 @@ +var gr = new GlideRecord('incident'); +gr.addQuery('state', 7); // Closed +gr.addQuery('active', false); +gr.addQuery('closed_at', '<=', gs.daysAgo(150)); +gr.query(); +while (gr.next()) { + var ar = new GlideRecord('ar_incident'); //ar_incident is the new table for storing archive data + ar.initialize(); + ar.short_description = gr.short_description; + ar.description = gr.description; + // Copy other necessary fields + ar.insert(); + gr.deleteRecord(); // deleting from incident table if record in inserted in the archived table +}