Skip to Content

Bulk Update Selected Records

Add a list action to bulk update selected records with common values or execute batch operations.

Table: incident
#ui-action #bulk-update #list-action #batch-processing #modal

Script Code

JavaScript
1// Client Script (runs in browser)
2if (typeof window != 'undefined') {
3  // Get selected records
4  var selectedItems = g_list.getChecked();
5  
6  if (selectedItems.length === 0) {
7    alert('Please select at least one record to update.');
8    return;
9  }
10  
11  // Confirm action
12  var confirmed = confirm('Are you sure you want to bulk update ' + selectedItems.length + ' selected records?');
13  if (!confirmed) {
14    return;
15  }
16  
17  // Show update dialog
18  var updateData = showBulkUpdateDialog();
19  if (!updateData) {
20    return;
21  }
22  
23  // Send to server for processing
24  var ajaxRequest = new GlideAjax('BulkUpdateProcessor');
25  ajaxRequest.addParam('sysparm_name', 'bulkUpdateIncidents');
26  ajaxRequest.addParam('sysparm_sys_ids', selectedItems.join(','));
27  ajaxRequest.addParam('sysparm_updates', JSON.stringify(updateData));
28  
29  ajaxRequest.getXMLAnswer(function(response) {
30    if (response) {
31      var result = JSON.parse(response);
32      if (result.success) {
33        alert('Successfully updated ' + result.updated_count + ' records.');
34        g_list.refresh();
35      } else {
36        alert('Error: ' + result.message);
37      }
38    }
39  });
40  
41  /**
42   * Show dialog to collect update values
43   */
44  function showBulkUpdateDialog() {
45    var dialog = new GlideModal('bulk_update_modal', true);
46    dialog.setTitle('Bulk Update Incidents');
47    
48    var html = '<div style="padding: 20px;">' +
49               '<form id="bulk_update_form">' +
50               '<div class="form-group">' +
51               '<label for="priority">Priority:</label>' +
52               '<select name="priority" class="form-control">' +
53               '<option value="">-- No Change --</option>' +
54               '<option value="1">1 - Critical</option>' +
55               '<option value="2">2 - High</option>' +
56               '<option value="3">3 - Moderate</option>' +
57               '<option value="4">4 - Low</option>' +
58               '<option value="5">5 - Planning</option>' +
59               '</select>' +
60               '</div>' +
61               '<div class="form-group">' +
62               '<label for="state">State:</label>' +
63               '<select name="state" class="form-control">' +
64               '<option value="">-- No Change --</option>' +
65               '<option value="1">New</option>' +
66               '<option value="2">In Progress</option>' +
67               '<option value="3">On Hold</option>' +
68               '<option value="6">Resolved</option>' +
69               '<option value="7">Closed</option>' +
70               '</select>' +
71               '</div>' +
72               '<div class="form-group">' +
73               '<label for="assignment_group">Assignment Group:</label>' +
74               '<input type="text" name="assignment_group" class="form-control" placeholder="Leave blank for no change" />' +
75               '</div>' +
76               '<div class="form-group">' +
77               '<label for="work_notes">Work Notes:</label>' +
78               '<textarea name="work_notes" class="form-control" rows="3" placeholder="Optional work notes to add"></textarea>' +
79               '</div>' +
80               '<div style="text-align: right; margin-top: 20px;">' +
81               '<button type="button" class="btn btn-default" onclick="closeBulkUpdateDialog()">Cancel</button>' +
82               '<button type="button" class="btn btn-primary" onclick="submitBulkUpdate()" style="margin-left: 10px;">Update Records</button>' +
83               '</div>' +
84               '</form>' +
85               '</div>';
86    
87    dialog.setBody(html);
88    dialog.render();
89    
90    // Global functions for dialog interaction
91    window.closeBulkUpdateDialog = function() {
92      dialog.destroy();
93      window.bulkUpdateResult = null;
94    };
95    
96    window.submitBulkUpdate = function() {
97      var form = document.getElementById('bulk_update_form');
98      var formData = new FormData(form);
99      var updateData = {};
100      
101      // Collect form values
102      for (var pair of formData.entries()) {
103        if (pair[1]) {  // Only include non-empty values
104          updateData[pair[0]] = pair[1];
105        }
106      }
107      
108      if (Object.keys(updateData).length === 0) {
109        alert('Please select at least one field to update.');
110        return;
111      }
112      
113      window.bulkUpdateResult = updateData;
114      dialog.destroy();
115    };
116    
117    // Wait for user interaction
118    var checkResult = setInterval(function() {
119      if (window.bulkUpdateResult !== undefined) {
120        clearInterval(checkResult);
121        return window.bulkUpdateResult;
122      }
123    }, 100);
124    
125    // Timeout after 30 seconds
126    setTimeout(function() {
127      if (window.bulkUpdateResult === undefined) {
128        clearInterval(checkResult);
129        dialog.destroy();
130        window.bulkUpdateResult = null;
131      }
132    }, 30000);
133  }
134  
135} else {
136  // Server Script (runs on server) - This part runs when action is executed
137  // The actual bulk update logic should be in a Script Include called 'BulkUpdateProcessor'
138  
139  action.setRedirectURL('incident_list.do?sysparm_query=' + current.getTableName() + '^ORDERBYnumber');
140}

How to Use

1. Create a UI Action on incident table\n2. Set Action name to 'bulk_update_incidents'\n3. Check 'List v3' checkbox\n4. Set Condition: gs.hasRole('itil') || gs.hasRole('admin')\n5. Create Script Include 'BulkUpdateProcessor' with 'bulkUpdateIncidents' function\n6. Test by selecting multiple records and clicking the action\n7. Customize form fields and validation as needed

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts