|
| 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 | + |
| 38 | +## Example Usage |
| 39 | +```javascript |
| 40 | +// Customize these variables: |
| 41 | +var TABLE = 'incident'; |
| 42 | +var FILTER = "priority=1^state=2"; // Your query condition |
| 43 | +var BATCH_SIZE = 100; |
| 44 | +var FIELD_TO_UPDATE = 'assignment_group'; // Field to update |
| 45 | +var NEW_VALUE = '123456789abc'; // New value |
| 46 | + |
| 47 | +// Run the script - it handles everything else |
| 48 | +``` |
| 49 | + |
| 50 | +## Key Features |
| 51 | +- **Batch Processing**: Prevents timeout by processing records in chunks |
| 52 | +- **Error Resilience**: Continues on error, logs details |
| 53 | +- **Progress Tracking**: Logs every N records updated |
| 54 | +- **Flexible**: Works with any table and field |
| 55 | +- **Safe**: Won't crash on individual record failures |
| 56 | +- **Auditable**: Detailed logging of all operations |
| 57 | + |
| 58 | +## Output Examples |
| 59 | +``` |
| 60 | +[Bulk Update Started] Processing incidents with filter: priority=1 |
| 61 | +[Progress] Updated 100 records successfully (5 errors) |
| 62 | +[Progress] Updated 200 records successfully (8 errors) |
| 63 | +[Bulk Update Complete] Total: 250 | Success: 242 | Errors: 8 |
| 64 | +[Failed Records] 7af24b9c: User already has assignment |
| 65 | +[Failed Records] 8bd35c8d: Invalid assignment group |
| 66 | +``` |
| 67 | + |
| 68 | +## Performance Notes |
| 69 | +- Batch size of 100 is optimal for most tables |
| 70 | +- Adjust batch size based on available resources |
| 71 | +- Run during maintenance windows for large updates |
| 72 | +- Monitor system logs during execution |
| 73 | + |
| 74 | +## Customization |
| 75 | +```javascript |
| 76 | +// Change batch size for your table size |
| 77 | +var BATCH_SIZE = 50; // For smaller batches |
| 78 | +var BATCH_SIZE = 200; // For larger tables |
| 79 | + |
| 80 | +// Different field update logic |
| 81 | +record.setValue(FIELD_TO_UPDATE, NEW_VALUE); |
| 82 | +// Or use gs.getProperty() for configuration |
| 83 | +``` |
| 84 | + |
| 85 | +## Requirements |
| 86 | +- ServiceNow instance |
| 87 | +- Access to Background Scripts or Scheduled Jobs |
| 88 | +- Write access to target table |
| 89 | +- Appropriate table and field permissions |
| 90 | + |
| 91 | +## Related APIs |
| 92 | +- [GlideRecord Query API](https://docs.servicenow.com/bundle/sandiego-application-development/page/app-store/dev_apps/concept/c_UsingGlideRecord.html) |
| 93 | +- [GlideSystem Logging](https://docs.servicenow.com/bundle/sandiego-application-development/page/app-store/dev_apps/concept/c_SystemLog.html) |
| 94 | +- [Best Practices for Bulk Operations](https://docs.servicenow.com/bundle/sandiego-application-development/page/app-store/dev_apps/concept/c_BulkOperations.html) |
0 commit comments