File tree Expand file tree Collapse file tree 2 files changed +65
-0
lines changed
Core ServiceNow APIs/GlideRecord/Safe Bulk Delete Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ # GlideRecord Bulk Delete with Safety Checks
2+
3+ ## Description
4+ This snippet allows you to safely delete multiple records from a ServiceNow table based on an encoded query.
5+ It logs all records that match the query so you can review them before actually deleting anything.
6+ Helps prevent accidental mass deletion of important data.
7+
8+ ## Note
9+ - Works in Global Scope by default
10+ - Can be executed in Background Scripts or Script Includes
11+ - ** ALWAYS REVIEW LOGS BEFORE ENABLING DELETION**
12+ ## Prerequisites
13+ - Server-side context (Background Script, Business Rule, Script Include)
14+ - Access to the target table
15+ - Basic understanding of GlideRecord and Encoded Queries
16+
17+ ## Usage
18+ ``` javascript
19+ // Logs all active low-priority incidents that would be deleted
20+ safeDelete (' incident' , ' active=true^priority=5' );
21+
22+ // To perform actual deletion, uncomment gr.deleteRecord() inside the function
23+ ```
24+
25+ ## Output
26+ ```
27+ Records matching query: 3
28+ Record sys_id: 12345abcdef would be deleted.
29+ Record sys_id: 23456bcdef would be deleted.
30+ Record sys_id: 34567cdefg would be deleted.
31+ Bulk delete preview complete. Verify logs before enabling deletion.
32+ ```
Original file line number Diff line number Diff line change 1+ /**
2+ * Safely delete multiple records from a ServiceNow table.
3+ * Logs all affected records before deletion to prevent accidental data loss.
4+ * Uncomment gr.deleteRecord() to perform the actual deletion.
5+ *
6+ * @param {string } table - The table name
7+ * @param {string } encodedQuery - GlideRecord encoded query for filtering records
8+ */
9+ function safeDelete ( table , encodedQuery ) {
10+ if ( ! table || ! encodedQuery ) {
11+ gs . error ( 'Both table name and encoded query are required.' ) ;
12+ return ;
13+ }
14+
15+ var gr = new GlideRecord ( table ) ;
16+ gr . addEncodedQuery ( encodedQuery ) ;
17+ gr . query ( ) ;
18+
19+ var count = gr . getRowCount ( ) ;
20+ gs . info ( 'Records matching query: ' + count ) ;
21+
22+ if ( count === 0 ) {
23+ gs . info ( 'No records found. Nothing to delete.' ) ;
24+ return ;
25+ }
26+
27+ while ( gr . next ( ) ) {
28+ gs . info ( 'Record sys_id: ' + gr . getValue ( 'sys_id' ) + ' would be deleted.' ) ;
29+ // gr.deleteRecord(); // Uncomment this line to actually delete
30+ }
31+
32+ gs . info ( 'Bulk delete preview complete. Verify logs before enabling deletion.' ) ;
33+ }
You can’t perform that action at this time.
0 commit comments