Skip to content

Commit 54f39ce

Browse files
authored
feature: Added Safe Bulk Record Update with Progress Tracking snippet (#2569)
1 parent fe3b963 commit 54f39ce

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Safe Bulk Record Update with Logging
2+
3+
## Overview
4+
Efficiently update multiple records in batch with error handling, progress tracking, and logging to prevent timeouts and data loss.
5+
6+
## What It Does
7+
- Updates records in configurable batch sizes
8+
- Logs progress for monitoring
9+
- Handles individual record errors without stopping batch
10+
- Prevents script timeout with batch processing
11+
- Tracks success/failure counts
12+
- Logs detailed error information
13+
14+
## Use Cases
15+
- Bulk data migrations
16+
- Mass field updates after deployment
17+
- Scheduled bulk corrections
18+
- Data cleanup operations
19+
- Batch status updates across records
20+
21+
## Files
22+
- `bulk_update_with_progress.js` - Background Script for safe bulk updates
23+
24+
## How to Use
25+
26+
### Option 1: Run as Background Script
27+
1. Go to **System Diagnostics > Script Background**
28+
2. Copy code from `bulk_update_with_progress.js`
29+
3. Modify the table name and query filter
30+
4. Execute and monitor logs
31+
32+
### Option 2: Create as Scheduled Job
33+
1. Go to **System Scheduler > Scheduled Jobs**
34+
2. Create new job with the script code
35+
3. Schedule for off-peak hours
36+
4. Logs will be available in System Logs
37+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var TABLE = 'incident'; // Change to your table
2+
var FILTER = "priority=1"; // Add your filter conditions
3+
var FIELD_TO_UPDATE = 'state'; // Field to update
4+
var NEW_VALUE = '1'; // Value to set
5+
6+
var successCount = 0;
7+
//using gs.info for best logging
8+
gs.info('[Bulk Update Started] Table: ' + TABLE + ' | Filter: ' + FILTER, 'BulkUpdate');
9+
10+
try {
11+
var gr = new GlideRecord(TABLE);
12+
gr.addEncodedQuery(FILTER);
13+
gr.query();
14+
15+
// Collect all record IDs first (single query)
16+
var recordIds = [];
17+
while (gr.next()) {
18+
recordIds.push(gr.getUniqueValue());
19+
}
20+
21+
//using updateMultiple will update all records at once
22+
if (recordIds.length > 0) {
23+
var updateGr = new GlideRecord(TABLE);
24+
updateGr.addEncodedQuery(FILTER);
25+
updateGr.setValue(FIELD_TO_UPDATE, NEW_VALUE);
26+
updateGr.updateMultiple();//this is more efficient
27+
successCount = recordIds.length;
28+
gs.info('[Bulk Update Complete] Total Updated: ' + successCount, 'BulkUpdate');
29+
} else {
30+
gs.info('[Bulk Update] No records matched the filter criteria', 'BulkUpdate');
31+
}
32+
} catch (e) {
33+
gs.error('[Bulk Update Error] ' + e.toString(), 'BulkUpdate');
34+
}

0 commit comments

Comments
 (0)