Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<javascript:gs.daysAgoStart(n)` or a literal date
- Limit fields with `addQuery` and `setLimit` when sampling

## References
- GlideRecord API
https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/GlideRecord/concept/c_GlideRecordAPI.html
- GlideDateTime API
https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/GlideDateTime/concept/c_GlideDateTimeAPI.html
- GlideSystem date helpers
https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/GlideSystem/reference/r_GlideSystem-date-methods.html
Original file line number Diff line number Diff line change
@@ -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);
})();
Loading