Skip to content

Commit 223bb7b

Browse files
committed
fixed: Optimized bulk update to use updateMultiple and reduce database queries
1 parent b35860e commit 223bb7b

File tree

1 file changed

+16
-36
lines changed

1 file changed

+16
-36
lines changed
Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,40 @@
11
// Background Script: Safe Bulk Record Update with Progress Tracking
2-
// Purpose: Update multiple records safely with batch processing and error handling
2+
// Purpose: Update multiple records efficiently using updateMultiple()
33

44
var TABLE = 'incident'; // Change to your table
55
var FILTER = "priority=1"; // Add your filter conditions
6-
var BATCH_SIZE = 100;
76
var FIELD_TO_UPDATE = 'state'; // Field to update
87
var NEW_VALUE = '1'; // Value to set
98

109
var successCount = 0;
1110
var errorCount = 0;
12-
var totalProcessed = 0;
1311

14-
gs.log('[Bulk Update Started] Table: ' + TABLE + ' | Filter: ' + FILTER, 'BulkUpdate');
12+
gs.info('[Bulk Update Started] Table: ' + TABLE + ' | Filter: ' + FILTER, 'BulkUpdate');
1513

1614
try {
1715
var gr = new GlideRecord(TABLE);
1816
gr.addEncodedQuery(FILTER);
1917
gr.query();
2018

21-
var recordsToProcess = [];
19+
// Collect all record IDs first (single query)
20+
var recordIds = [];
2221
while (gr.next()) {
23-
recordsToProcess.push(gr.getUniqueValue());
24-
25-
// Process in batches to prevent timeout
26-
if (recordsToProcess.length === BATCH_SIZE) {
27-
processBatch(recordsToProcess);
28-
recordsToProcess = [];
29-
}
22+
recordIds.push(gr.getUniqueValue());
3023
}
3124

32-
// Process remaining records
33-
if (recordsToProcess.length > 0) {
34-
processBatch(recordsToProcess);
25+
// Update all records at once using updateMultiple
26+
if (recordIds.length > 0) {
27+
var updateGr = new GlideRecord(TABLE);
28+
updateGr.addEncodedQuery(FILTER);
29+
updateGr.setValue(FIELD_TO_UPDATE, NEW_VALUE);
30+
updateGr.updateMultiple();
31+
32+
successCount = recordIds.length;
33+
gs.info('[Bulk Update Complete] Total Updated: ' + successCount, 'BulkUpdate');
34+
} else {
35+
gs.info('[Bulk Update] No records matched the filter criteria', 'BulkUpdate');
3536
}
3637

37-
gs.log('[Bulk Update Complete] Total: ' + totalProcessed + ' | Success: ' + successCount + ' | Errors: ' + errorCount, 'BulkUpdate');
38-
3938
} catch (e) {
4039
gs.error('[Bulk Update Error] ' + e.toString(), 'BulkUpdate');
4140
}
42-
43-
function processBatch(recordIds) {
44-
for (var i = 0; i < recordIds.length; i++) {
45-
try {
46-
var record = new GlideRecord(TABLE);
47-
record.get(recordIds[i]);
48-
record.setValue(FIELD_TO_UPDATE, NEW_VALUE);
49-
record.update();
50-
successCount++;
51-
} catch (error) {
52-
errorCount++;
53-
gs.log('[Failed Record] ' + recordIds[i] + ': ' + error.toString(), 'BulkUpdate');
54-
}
55-
totalProcessed++;
56-
}
57-
58-
// Log progress
59-
gs.log('[Progress] Updated ' + totalProcessed + ' records | Success: ' + successCount + ' | Errors: ' + errorCount, 'BulkUpdate');
60-
}

0 commit comments

Comments
 (0)