From 64d9fd540f177a01faa8035682c6c2919c0aaca8 Mon Sep 17 00:00:00 2001 From: Rajasree2004 Date: Sat, 25 Oct 2025 17:17:47 +0530 Subject: [PATCH] Add Safe Bulk Delete --- .../GlideRecord/Safe Bulk Delete/README.md | 32 ++++++++++++++++++ .../Safe Bulk Delete/safeDelete.js | 33 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 Core ServiceNow APIs/GlideRecord/Safe Bulk Delete/README.md create mode 100644 Core ServiceNow APIs/GlideRecord/Safe Bulk Delete/safeDelete.js diff --git a/Core ServiceNow APIs/GlideRecord/Safe Bulk Delete/README.md b/Core ServiceNow APIs/GlideRecord/Safe Bulk Delete/README.md new file mode 100644 index 0000000000..069bc4b7d0 --- /dev/null +++ b/Core ServiceNow APIs/GlideRecord/Safe Bulk Delete/README.md @@ -0,0 +1,32 @@ +# GlideRecord Bulk Delete with Safety Checks + +## Description +This snippet allows you to safely delete multiple records from a ServiceNow table based on an encoded query. +It logs all records that match the query so you can review them before actually deleting anything. +Helps prevent accidental mass deletion of important data. + +## Note +- Works in Global Scope by default +- Can be executed in Background Scripts or Script Includes +- **ALWAYS REVIEW LOGS BEFORE ENABLING DELETION** +## Prerequisites +- Server-side context (Background Script, Business Rule, Script Include) +- Access to the target table +- Basic understanding of GlideRecord and Encoded Queries + +## Usage +```javascript +// Logs all active low-priority incidents that would be deleted +safeDelete('incident', 'active=true^priority=5'); + +// To perform actual deletion, uncomment gr.deleteRecord() inside the function +``` + +## Output +``` +Records matching query: 3 +Record sys_id: 12345abcdef would be deleted. +Record sys_id: 23456bcdef would be deleted. +Record sys_id: 34567cdefg would be deleted. +Bulk delete preview complete. Verify logs before enabling deletion. +``` diff --git a/Core ServiceNow APIs/GlideRecord/Safe Bulk Delete/safeDelete.js b/Core ServiceNow APIs/GlideRecord/Safe Bulk Delete/safeDelete.js new file mode 100644 index 0000000000..24417876c2 --- /dev/null +++ b/Core ServiceNow APIs/GlideRecord/Safe Bulk Delete/safeDelete.js @@ -0,0 +1,33 @@ +/** + * Safely delete multiple records from a ServiceNow table. + * Logs all affected records before deletion to prevent accidental data loss. + * Uncomment gr.deleteRecord() to perform the actual deletion. + * + * @param {string} table - The table name + * @param {string} encodedQuery - GlideRecord encoded query for filtering records + */ +function safeDelete(table, encodedQuery) { + if (!table || !encodedQuery) { + gs.error('Both table name and encoded query are required.'); + return; + } + + var gr = new GlideRecord(table); + gr.addEncodedQuery(encodedQuery); + gr.query(); + + var count = gr.getRowCount(); + gs.info('Records matching query: ' + count); + + if (count === 0) { + gs.info('No records found. Nothing to delete.'); + return; + } + + while (gr.next()) { + gs.info('Record sys_id: ' + gr.getValue('sys_id') + ' would be deleted.'); + // gr.deleteRecord(); // Uncomment this line to actually delete + } + + gs.info('Bulk delete preview complete. Verify logs before enabling deletion.'); +}