From 31d7347084dfce322e3ca1c3ad0de3c374598964 Mon Sep 17 00:00:00 2001 From: Lucifer <108731648+shivamvish160@users.noreply.github.com> Date: Wed, 15 Oct 2025 17:19:54 +0530 Subject: [PATCH 1/4] Create README.md README.md file for description --- .../Dynamic Record Archiving Logic/README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Server-Side Components/Script Includes/Dynamic Record Archiving Logic/README.md diff --git a/Server-Side Components/Script Includes/Dynamic Record Archiving Logic/README.md b/Server-Side Components/Script Includes/Dynamic Record Archiving Logic/README.md new file mode 100644 index 0000000000..78173c6f7b --- /dev/null +++ b/Server-Side Components/Script Includes/Dynamic Record Archiving Logic/README.md @@ -0,0 +1,32 @@ +# 🗃️ Dynamic Record Archiving Logic + +## 📌 Overview +This snippet provides a reusable logic to archive or flag records in ServiceNow that are older than a specified threshold. It helps maintain data hygiene, improve performance, and support compliance with data retention policies. + +You can choose between two modes: +- **Flag Mode**: Marks records as archived using a custom field (`u_archived`). +- **Move Mode**: Moves records to a corresponding archive table (e.g., `incident_archive`) and deletes the original. + +--- + +## 🧠 Use Case +- Archive incidents older than 1 year. +- Clean up old tasks, requests, or custom records. +- Automate data lifecycle management via Scheduled Jobs. + +--- + +## ⚙️ Parameters +| Parameter | Description | +|---------------------|-----------------------------------------------------------------------------| +| `tableName` | Name of the table to archive (e.g., `incident`) | +| `dateField` | Date field to filter by (e.g., `opened_at`, `created_on`) | +| `archiveThresholdDays` | Number of days before today to consider for archiving (e.g., `365`) | +| `archiveMode` | `'flag'` to mark records, `'move'` to transfer to archive table | + +--- + +## 🧩 Example Usage +```javascript +var archiver = new RecordArchiver('incident', 'opened_at', 365, 'flag'); +archiver.archiveRecords(); From f51aebf5422806805921a5c7c8f33ca13feb5070 Mon Sep 17 00:00:00 2001 From: Lucifer <108731648+shivamvish160@users.noreply.github.com> Date: Wed, 15 Oct 2025 17:22:04 +0530 Subject: [PATCH 2/4] Delete Server-Side Components/Script Includes/Dynamic Record Archiving Logic directory --- .../Dynamic Record Archiving Logic/README.md | 32 ------------------- 1 file changed, 32 deletions(-) delete mode 100644 Server-Side Components/Script Includes/Dynamic Record Archiving Logic/README.md diff --git a/Server-Side Components/Script Includes/Dynamic Record Archiving Logic/README.md b/Server-Side Components/Script Includes/Dynamic Record Archiving Logic/README.md deleted file mode 100644 index 78173c6f7b..0000000000 --- a/Server-Side Components/Script Includes/Dynamic Record Archiving Logic/README.md +++ /dev/null @@ -1,32 +0,0 @@ -# 🗃️ Dynamic Record Archiving Logic - -## 📌 Overview -This snippet provides a reusable logic to archive or flag records in ServiceNow that are older than a specified threshold. It helps maintain data hygiene, improve performance, and support compliance with data retention policies. - -You can choose between two modes: -- **Flag Mode**: Marks records as archived using a custom field (`u_archived`). -- **Move Mode**: Moves records to a corresponding archive table (e.g., `incident_archive`) and deletes the original. - ---- - -## 🧠 Use Case -- Archive incidents older than 1 year. -- Clean up old tasks, requests, or custom records. -- Automate data lifecycle management via Scheduled Jobs. - ---- - -## ⚙️ Parameters -| Parameter | Description | -|---------------------|-----------------------------------------------------------------------------| -| `tableName` | Name of the table to archive (e.g., `incident`) | -| `dateField` | Date field to filter by (e.g., `opened_at`, `created_on`) | -| `archiveThresholdDays` | Number of days before today to consider for archiving (e.g., `365`) | -| `archiveMode` | `'flag'` to mark records, `'move'` to transfer to archive table | - ---- - -## 🧩 Example Usage -```javascript -var archiver = new RecordArchiver('incident', 'opened_at', 365, 'flag'); -archiver.archiveRecords(); From 3d66b443c2b84d926fe3ad423e5b01b69b3ca0c0 Mon Sep 17 00:00:00 2001 From: Lucifer <108731648+shivamvish160@users.noreply.github.com> Date: Wed, 15 Oct 2025 17:33:37 +0530 Subject: [PATCH 3/4] Create README.md --- .../Recursive GlideRecord Fetcher/README.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Server-Side Components/Script Includes/Recursive GlideRecord Fetcher/README.md diff --git a/Server-Side Components/Script Includes/Recursive GlideRecord Fetcher/README.md b/Server-Side Components/Script Includes/Recursive GlideRecord Fetcher/README.md new file mode 100644 index 0000000000..a8926f2059 --- /dev/null +++ b/Server-Side Components/Script Includes/Recursive GlideRecord Fetcher/README.md @@ -0,0 +1,29 @@ +# Recursive GlideRecord Fetcher +## Overview +This snippet provides a reusable logic to recursively fetch child records from a parent record in ServiceNow. It is useful for traversing hierarchical relationships such as tasks, categories, CMDB CI relationships, or any table with a parent-child structure. + +The logic prevents infinite loops by tracking visited records and supports nesting of children for structured output. + +--- + +## Use Case +- Fetch all subtasks under a parent task. +- Traverse CMDB CI relationships recursively. +- Build hierarchical views of organizational units or categories. + +--- + +## Parameters +| Parameter | Description | +|------------------|-----------------------------------------------------------------------------| +| `tableName` | Name of the table to query (e.g., `task`, `cmdb_ci`, `custom_table`) | +| `parentField` | Field that links to the parent record (e.g., `parent`, `parent_id`) | +| `parentSysId` | Sys ID of the root parent record to start traversal | + +--- + +## Example Usage +```javascript +var fetcher = new RecursiveFetcher('task', 'parent'); +var hierarchy = fetcher.fetchChildren('abc123sysid'); // Replace with actual parent sys_id +gs.info(JSON.stringify(hierarchy)); From 40400962058f62765d1f75257a912003abcc8a8b Mon Sep 17 00:00:00 2001 From: Lucifer <108731648+shivamvish160@users.noreply.github.com> Date: Wed, 15 Oct 2025 17:34:18 +0530 Subject: [PATCH 4/4] Create code.js --- .../Recursive GlideRecord Fetcher/code.js | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Server-Side Components/Script Includes/Recursive GlideRecord Fetcher/code.js diff --git a/Server-Side Components/Script Includes/Recursive GlideRecord Fetcher/code.js b/Server-Side Components/Script Includes/Recursive GlideRecord Fetcher/code.js new file mode 100644 index 0000000000..8e042e9d35 --- /dev/null +++ b/Server-Side Components/Script Includes/Recursive GlideRecord Fetcher/code.js @@ -0,0 +1,41 @@ +var RecursiveFetcher = Class.create(); +RecursiveFetcher.prototype = { + initialize: function(tableName, parentField) { + this.tableName = tableName; + this.parentField = parentField; + this.visited = []; + }, + + fetchChildren: function(parentSysId) { + if (this.visited.indexOf(parentSysId) !== -1) { + // Avoid infinite loops + return []; + } + + this.visited.push(parentSysId); + var children = []; + + var gr = new GlideRecord(this.tableName); + gr.addQuery(this.parentField, parentSysId); + gr.query(); + + while (gr.next()) { + var child = { + sys_id: gr.getValue('sys_id'), + name: gr.getDisplayValue('name') || gr.getDisplayValue('short_description'), + children: this.fetchChildren(gr.getValue('sys_id')) // Recursive call + }; + children.push(child); + } + + return children; + }, + + type: 'RecursiveFetcher' +}; + +// Example usage: +//var fetcher = new RecursiveFetcher('task', 'parent'); +//var hierarchy = fetcher.fetchChildren('abc123sysid'); // Replace with actual parent sys_id +//gs.info(JSON.stringify(hierarchy)); +``