One very useful feature in ServiceNow is the “Force to Update Set” UI Action (found here). The only downside to this is that it only adds one record at a time. There have been several times where I’ve had to add multiple records to an update set. To that end here is the full code to implement a UI action that allows adding a list of records to an update set:

Force List to Update Set UI Action
Name: Force List to Update Set
Table:Global
Action name:
Client: True
List context menu: True
OnClick: forceToUpdateSet();
Script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function forceToUpdateSet() {
	var tablename = g_list.getTableName();
	var query = g_list.getQuery();
	var jax = new GlideAjax('ForceToUpdateSet');
	jax.addParam('sysparm_name', 'forceToUpdateSet');
	jax.addParam('sysparm_tablename', tablename);
	jax.addParam('sysparm_query', query);
	jax.getXML(function(resp) {
		var answer = resp.responseXML.documentElement.getAttribute("answer");
		alert(answer);
		g_list.refresh();
	});
	
}
Script Include;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var ForceToUpdateSet = Class.create();
ForceToUpdateSet.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	forceToUpdateSet : function() {
		var tablename = this.getParameter('sysparm_tablename');
		var query = this.getParameter('sysparm_query');
		return this.forceListToUpdateSet(tablename, query);
	},
	
	forceListToUpdateSet : function(tableName, theQuery) {
		
		var recordList = new GlideRecord(tableName);
		
		if (tableName.startsWith('wf_') || tableName.startsWith('sys_ui_') || tableName == 'sys_choice' ||
			(recordList.getED().hasAttribute('update_synch') && recordList.getED().getAttribute('update_synch') == 'true')
		|| recordList.getED().hasAttribute('update_synch_custom')) {
			gs.addErrorMessage('Updates are already being recorded for this table.');
			return 'Updates are already being recorded for this table.';
		}
		
		try {
			
			var um = new GlideUpdateManager2();
			
			recordList.addEncodedQuery(theQuery);
			recordList.query();
			while (recordList.next()) {
				um.saveRecord(recordList);
			}
			
			var setID = gs.getPreference('sys_update_set');
			var us = new GlideRecord('sys_update_set');
			us.get(setID);
			return 'Records included in ' + us.name + ' update set.';
		}
		catch (ex) {
			return 'There was an error adding records to the update set: ' + ex;
		}
		
		
	},
	type: 'ForceToUpdateSet'
});