From c96fc803bcc775fd391d6d82510e94cd5e3c4828 Mon Sep 17 00:00:00 2001 From: hanna-g-sn Date: Tue, 21 Oct 2025 09:28:11 +0100 Subject: [PATCH 1/2] Add README for querying old records with GlideRecord This README provides a guide on querying records older than a specified number of days using GlideRecord in ServiceNow. It includes details on the solution, usage context, implementation steps, and references to relevant APIs. --- .../Get Records Older than X Days/README.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Core ServiceNow APIs/GlideRecord/Get Records Older than X Days/README.md diff --git a/Core ServiceNow APIs/GlideRecord/Get Records Older than X Days/README.md b/Core ServiceNow APIs/GlideRecord/Get Records Older than X Days/README.md new file mode 100644 index 0000000000..f29bfa3419 --- /dev/null +++ b/Core ServiceNow APIs/GlideRecord/Get Records Older than X Days/README.md @@ -0,0 +1,23 @@ +# Query records older than N days efficiently + +## What this solves +You often need to find records older than a threshold without loading the world into memory. This snippet: +- Uses GlideDateTime maths to compute a date boundary +- Uses encoded queries with index-friendly operators +- Streams results and logs a compact summary + +## Where to use +Background Script, Script Include utility, or any server-side context. + +## How it works +- Compute a past date with GlideDateTime and subtract days +- Build an encoded query like `opened_at Date: Tue, 21 Oct 2025 09:28:59 +0100 Subject: [PATCH 2/2] Add script to get incident records older than 30 days This script retrieves records from the 'incident' table that are older than 30 days based on the 'opened_at' field. It limits the output to 200 records and provides a count of the retrieved records. --- .../gr_older_than_n_days.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Core ServiceNow APIs/GlideRecord/Get Records Older than X Days/gr_older_than_n_days.js diff --git a/Core ServiceNow APIs/GlideRecord/Get Records Older than X Days/gr_older_than_n_days.js b/Core ServiceNow APIs/GlideRecord/Get Records Older than X Days/gr_older_than_n_days.js new file mode 100644 index 0000000000..44aab9c9a3 --- /dev/null +++ b/Core ServiceNow APIs/GlideRecord/Get Records Older than X Days/gr_older_than_n_days.js @@ -0,0 +1,24 @@ +// Background Script or Script Include method +(function() { + var TABLE = 'incident'; // table to query + var FIELD = 'opened_at'; // date-time field to compare + var DAYS = 30; // older than N days + + var gdt = new GlideDateTime(); + gdt.addDaysUTC(-DAYS); // move back N days in UTC + + var gr = new GlideRecord(TABLE); + gr.addQuery(FIELD, '<=', gdt.getValue()); // index-friendly comparison + gr.addActiveQuery(); // example extra filter + gr.setLimit(200); // sample to avoid huge output + gr.query(); + + var count = 0; + while (gr.next()) { + count++; + // do work or collect ids + // gs.info(gr.getUniqueValue() + ' | ' + gr.getDisplayValue(FIELD)); + } + + gs.info(TABLE + ' older than ' + DAYS + ' days: ' + count); +})();