diff --git a/Server-Side Components/Background Scripts/Safe Bulk Update with Logging/README.md b/Server-Side Components/Background Scripts/Safe Bulk Update with Logging/README.md new file mode 100644 index 0000000000..87b5b84c7c --- /dev/null +++ b/Server-Side Components/Background Scripts/Safe Bulk Update with Logging/README.md @@ -0,0 +1,37 @@ +# Safe Bulk Record Update with Logging + +## Overview +Efficiently update multiple records in batch with error handling, progress tracking, and logging to prevent timeouts and data loss. + +## What It Does +- Updates records in configurable batch sizes +- Logs progress for monitoring +- Handles individual record errors without stopping batch +- Prevents script timeout with batch processing +- Tracks success/failure counts +- Logs detailed error information + +## Use Cases +- Bulk data migrations +- Mass field updates after deployment +- Scheduled bulk corrections +- Data cleanup operations +- Batch status updates across records + +## Files +- `bulk_update_with_progress.js` - Background Script for safe bulk updates + +## How to Use + +### Option 1: Run as Background Script +1. Go to **System Diagnostics > Script Background** +2. Copy code from `bulk_update_with_progress.js` +3. Modify the table name and query filter +4. Execute and monitor logs + +### Option 2: Create as Scheduled Job +1. Go to **System Scheduler > Scheduled Jobs** +2. Create new job with the script code +3. Schedule for off-peak hours +4. Logs will be available in System Logs + diff --git a/Server-Side Components/Background Scripts/Safe Bulk Update with Logging/bulk_update_with_progress.js b/Server-Side Components/Background Scripts/Safe Bulk Update with Logging/bulk_update_with_progress.js new file mode 100644 index 0000000000..5bf9a6e5c8 --- /dev/null +++ b/Server-Side Components/Background Scripts/Safe Bulk Update with Logging/bulk_update_with_progress.js @@ -0,0 +1,34 @@ +var TABLE = 'incident'; // Change to your table +var FILTER = "priority=1"; // Add your filter conditions +var FIELD_TO_UPDATE = 'state'; // Field to update +var NEW_VALUE = '1'; // Value to set + +var successCount = 0; +//using gs.info for best logging +gs.info('[Bulk Update Started] Table: ' + TABLE + ' | Filter: ' + FILTER, 'BulkUpdate'); + +try { + var gr = new GlideRecord(TABLE); + gr.addEncodedQuery(FILTER); + gr.query(); + + // Collect all record IDs first (single query) + var recordIds = []; + while (gr.next()) { + recordIds.push(gr.getUniqueValue()); + } + + //using updateMultiple will update all records at once + if (recordIds.length > 0) { + var updateGr = new GlideRecord(TABLE); + updateGr.addEncodedQuery(FILTER); + updateGr.setValue(FIELD_TO_UPDATE, NEW_VALUE); + updateGr.updateMultiple();//this is more efficient + successCount = recordIds.length; + gs.info('[Bulk Update Complete] Total Updated: ' + successCount, 'BulkUpdate'); + } else { + gs.info('[Bulk Update] No records matched the filter criteria', 'BulkUpdate'); + } +} catch (e) { + gs.error('[Bulk Update Error] ' + e.toString(), 'BulkUpdate'); +}