Skip to Content

Bulk Update Selected Records (List Action)

Update multiple selected list records with custom field values, useful for mass updates.

List Button Table: incident
#ui-action #list-button #bulk-update #mass-update #server-side #list-action

Script Code

JavaScript
1// Server-side code
2(function() {
3  // Get selected record sys_ids from URL parameter
4  var sysIds = g_request.getParameter('sysparm_record_list');
5
6  if (!sysIds) {
7    gs.addErrorMessage('No records selected');
8    return;
9  }
10
11  // Split comma-separated sys_ids
12  var recordIds = sysIds.split(',');
13
14  if (recordIds.length === 0) {
15    gs.addErrorMessage('No records selected');
16    return;
17  }
18
19  // Configuration: Define updates to apply
20  // Customize these values for your use case
21  var updates = {
22    'state': '2',              // In Progress
23    'assigned_to': gs.getUserID(),  // Assign to current user
24    'work_notes': 'Bulk updated by ' + gs.getUserName() + ' on ' + new GlideDateTime().getDisplayValue()
25  };
26
27  // Optional: Get update values from UI Page or dialog
28  // var newState = g_request.getParameter('sysparm_new_state');
29  // var newAssignedTo = g_request.getParameter('sysparm_assigned_to');
30
31  var successCount = 0;
32  var failCount = 0;
33  var errors = [];
34
35  // Process each selected record
36  for (var i = 0; i < recordIds.length; i++) {
37    var sysId = recordIds[i];
38
39    try {
40      var gr = new GlideRecord('incident');
41      if (gr.get(sysId)) {
42
43        // Apply updates
44        for (var field in updates) {
45          if (updates.hasOwnProperty(field)) {
46            gr.setValue(field, updates[field]);
47          }
48        }
49
50        // Validation: Check business rules before updating
51        // Example: Don't update if already closed
52        if (gr.state.toString() === '7') {  // Closed
53          errors.push(gr.number + ' is already closed');
54          failCount++;
55          continue;
56        }
57
58        // Update the record
59        gr.update();
60        successCount++;
61
62      } else {
63        errors.push('Record ' + sysId + ' not found');
64        failCount++;
65      }
66    } catch (e) {
67      errors.push('Error updating ' + sysId + ': ' + e.message);
68      failCount++;
69    }
70  }
71
72  // Show results
73  if (successCount > 0) {
74    gs.addInfoMessage('Successfully updated ' + successCount + ' record(s)');
75  }
76
77  if (failCount > 0) {
78    gs.addErrorMessage('Failed to update ' + failCount + ' record(s)');
79
80    // Show first 5 errors
81    var errorMsg = errors.slice(0, 5).join(', ');
82    if (errors.length > 5) {
83      errorMsg += ' and ' + (errors.length - 5) + ' more';
84    }
85    gs.addErrorMessage('Errors: ' + errorMsg);
86  }
87
88  // Return to list
89  action.setRedirectURL('incident_list.do');
90
91})();

How to Use

1. Create a new UI Action on your table 2. Set Name to 'Bulk Update' 3. Check 'List button' checkbox 4. Uncheck 'Client' checkbox 5. Set Order: 100 6. Configure updates object with your desired field values 7. Add condition if needed (e.g., gs.hasRole('admin')) 8. Test by selecting multiple records and clicking button 9. Consider creating a UI Page for dynamic input values 10. Add logging for audit trail if needed

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts