Schedule a script to run as a specific user.
Can be used to avoid "System" updating a record, when you know which user actually updated. Example usage:

var ssa = new ScheduleScriptAs('myScriptName');
ssa.setRunAs('USER_SYS_ID');
ssa.setScript('SCRIPT_TO_RUN');
ssa.runSecondsLater(10); //Add delay
ssa.schedule();

Script include:
var ScheduleScriptAs = Class.create();
ScheduleScriptAs.prototype = {
    initialize: function(name) {
		this.active = 'true';
		this.name = 'Run ScheduleScriptAs ' + name;
		this.run_type = 'once';
		this.run_start = new GlideDateTime();
		this.script = '';
		this.run_as = gs.getUserID();
		this.sys_name = this.name;
		this.sys_class_name = 'sysauto_script';
		this.runSecondsLater = 0;
    },
 
	runSecondsLater: function(seconds) {
		this.runSecondsLater += seconds;
		this.run_start = new GlideDateTime(this.run_start);
		this.run_start.addSeconds(seconds);
	},
 
	setRunAs: function(user_sys_id) {
		this.run_as = user_sys_id;
	},
 
	setScript: function(script) {
		this.script = script;
	},
 
	schedule: function() {
		var s = new GlideRecord(this.sys_class_name);
		s.initialize();
		s.active = this.active;
		s.name = this.name;
		s.run_type = this.run_type;
		s.run_start = this.run_start;
		s.script = this.script;
		s.run_as = this.run_as;
		s.sys_name = this.sys_name;
		var s_id = s.insert();
		this._deleteSchedule(s_id);
	},
 
	_deleteSchedule: function(sys_id) {
		gs.sleep(10000 + this.runSecondsLater); //Allow schedule to be run
		var s = new GlideRecord(this.sys_class_name);
		if(s.get(sys_id)) {
			s.deleteRecord();
		}
	},
 
 
    type: 'ScheduleScriptAs'
};


If you have ever been in a situation where you or someone else has clicked on the "Track in update sets" related link, then you seem to be stuck with it, and you keep tracking any updates to these records on the table.

In order to disable it, you need to run the below script as a Background Script. Of course you need to replace the TABLE_SYS_ID with your actual sys_id of the table you want to change.

doNotTrackInUpdateSets();
function doNotTrackInUpdateSets() {
	var gr = new GlideRecord('sys_db_object');
	gr.addQuery('sys_id', 'TABLE_SYS_ID'); // Replace TABLE_SYS_ID with your tables sys_id
	gr.query();
	while(gr.next()) {
		gr.super_class = '';
		gr.setWorkflow(false);
		gr.update();
	}
}


We all know and use GlideRecord.update() and GlideRecord.insert(), but sometimes we would like to do a little more than that, we would like to insert or update data in reference fields as well, now instead of creating a new query to get and update these fields, ServiceNow have created these two wonderful functions to deal with this issue.

There are a few things to take note of when using these methods.
Firstly that the related records are updated first and then the actual record afterwards.
Secondly you need to keep in mind, that if a related record does no exist, one will be created. (In the below sample code, if there are no caller, it will create one with the information provided in the script).
Also if you need to set the value of a reference field on the record, you must set it using the .setDisplayValue() method before manipulating the reference field's values.
Lastly remember to use these methods carefully, as it will update multiple tables at once, if that is what you tell it to do.

GlideRecord.updateWithReferences()

var inc = new GlideRecord('incident');
inc.get(sys_id); //Get incident record based on the sys_id
inc.caller_id.first_name = 'Jesper';
inc.caller_id.email= 'jesper@not-a-real-email.com';
inc.updateWithReferences();

GlideRecord.insertWithReferences()
var inc = new GlideRecord('incident');
inc.initialize();
inc.short_description = 'My printer is broken';
inc.caller_id.setDisplayValue('Jesper Testuser'); //Note that if we did not include this line to set the value, a new user would be created
inc.caller_id.first_name = 'Jesper';
inc.caller_id.email= 'jesper@not-a-real-email.com';
inc.insertWithReferences();
Read more about these functions at the ServiceNow Wiki

Top Cheat Sheets

About

ServiceHow.com's aim is to be a helpful resource for ServiceNow Developers. ServiceHow.com is not affiliated with ServiceNow.