|
1 | 1 | // 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() |
3 | 3 |
|
4 | 4 | var TABLE = 'incident'; // Change to your table |
5 | 5 | var FILTER = "priority=1"; // Add your filter conditions |
6 | | -var BATCH_SIZE = 100; |
7 | 6 | var FIELD_TO_UPDATE = 'state'; // Field to update |
8 | 7 | var NEW_VALUE = '1'; // Value to set |
9 | 8 |
|
10 | 9 | var successCount = 0; |
11 | 10 | var errorCount = 0; |
12 | | -var totalProcessed = 0; |
13 | 11 |
|
14 | | -gs.log('[Bulk Update Started] Table: ' + TABLE + ' | Filter: ' + FILTER, 'BulkUpdate'); |
| 12 | +gs.info('[Bulk Update Started] Table: ' + TABLE + ' | Filter: ' + FILTER, 'BulkUpdate'); |
15 | 13 |
|
16 | 14 | try { |
17 | 15 | var gr = new GlideRecord(TABLE); |
18 | 16 | gr.addEncodedQuery(FILTER); |
19 | 17 | gr.query(); |
20 | 18 |
|
21 | | - var recordsToProcess = []; |
| 19 | + // Collect all record IDs first (single query) |
| 20 | + var recordIds = []; |
22 | 21 | 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()); |
30 | 23 | } |
31 | 24 |
|
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'); |
35 | 36 | } |
36 | 37 |
|
37 | | - gs.log('[Bulk Update Complete] Total: ' + totalProcessed + ' | Success: ' + successCount + ' | Errors: ' + errorCount, 'BulkUpdate'); |
38 | | - |
39 | 38 | } catch (e) { |
40 | 39 | gs.error('[Bulk Update Error] ' + e.toString(), 'BulkUpdate'); |
41 | 40 | } |
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