ServiceNow Certification Mock Exams
Click To View Now
SNOWYCODE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
GlideRecord Cheat Sheet
Cheat Sheets

GlideRecord Cheat Sheet

GlideRecord Cheat Sheet

Below is a list of commonly used GlideRecord code that you can come back to daily while writing scripts in ServiceNow.

The examples are in no specific order - so just ctrl+f or cmd+f and search to find what you need!

Query

Basic GlideRecord query

 
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.query();
while(gr.next()) {
	gs.log('Category is ' + gr.category);
}

Get (sys_id)

Get a single GlideRecord with sys_id. A great way to return a single record when you know the sys_id of that record.

 
var gr = new GlideRecord('incident');
gr.get('sys_id'); // pass in the sys_id of the record you want
//Do something with the record returned
if(gr.category == 'hardware'){
gs.log('Category is ' + gr.category);
}

OR

The standard ‘addQuery’ parameter acts like an ‘and’ condition in your query. This example shows how you can add ‘or’ conditions to your query.

 var gr = new GlideRecord('incident');
var orGr = gr.addQuery('state', 6);
orGr.addOrCondition('state', 7);
gr.query();
while(gr.next()) {
	gs.log('Category is ' + gr.category);
}

OR (the simple way)

In addition to the example above this, you can also chain your ‘OR’ condition like below, which is usually simpler

 //Find all incidents with a priority of 1 or 2
var gr = new GlideRecord('incident');
gr.addQuery('priority', 1).addOrCondition('priority', 2);
gr.query();

Insert

Insert a new record

 //Create a new Incident record and populate the fields with the values below
var gr = new GlideRecord('incident');
gr.initialize();
gr.short_description = 'Wifi problem';
gr.category = 'network';
gr.caller_id.setDisplayValue('Steph Curry');
gr.insert();

Update

Update one or many records by querying the records, setting the values on the records, and then calling .update();

 //Find all active incident records and make them inactive
var gr = new GlideRecord('incident');
gr.addQuery('active',true);
gr.query();
while (gr.next()) {
gr.active = false;
gr.update();
}

Delete

Delete one or many records by querying for the records and then calling the .deleteRecord() method

 //Find all inactive incident records and delete them one-by-one
var gr = new GlideRecord('incident');
gr.addQuery('active',false);
gr.query();
while (gr.next()) {
//Delete each record in the query result set
gr.deleteRecord();
}

deleteMultiple (shortcut)

If you are deleting multiple records then the ‘deleteMultiple’ method can be used as a shortcut

 //Find all inactive incidents and delete them all at once
var gr = new GlideRecord('incident');
gr.addQuery('active', false);
gr.deleteMultiple(); //Deletes all records in the record set

addEncodedQuery

Encoded query strings can be copied directly from a filter, by right-clicking on the breadcrumbs

 var gr = new GlideRecord('incident');
gr.addEncodedQuery('active=true^state=2'); // Encoded query
gr.query();
while(gr.next()) {
	gs.log('Category is ' + gr.category);
}

GlideAggregate

Aggregates include COUNT, SUM, MIN, MAX, AVG

 //Find all active incidents and log a count of records to the system log
var gr = new GlideAggregate('incident');
gr.addQuery('active', true);
gr.addAggregate('COUNT');
gr.query();
var incidents = 0;
if (gr.next()){
incidents = gr.getAggregate('COUNT');
gs.log('Active incident count: ' + incidents);
}

orderBy/orderByDesc

Order the results of your recordset by using ‘orderBy’ and/or ‘orderByDesc’ as shown below.

 //Find all active incidents and order the results ascending by category then descending by created date
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.orderBy('category');
gr.orderByDesc('sys_created_on');
gr.query();

addNullQuery

Used to search for empty values

 //Find all incidents where the Short Description is empty
var gr = new GlideRecord('incident');
gr.addNullQuery('short_description');
gr.query();

addNotNullQuery

Used to search for not empty values

 //Find all incidents where the Short Description is not empty
var gr = new GlideRecord('incident');
gr.addNotNullQuery('short_description');
gr.query();

getRowCount

Used to get the number of results returned. Not available client-side.

 //Log the number of records returned by the query
var gr = new GlideRecord('incident');
gr.addQuery('category', 'software');
gr.query();
gs.log('Incident count: ' + gr.getRowCount());

‘getRowCount’ isn’t available client-side, however you can return the number of results in a client-side GlideRecord query by using ‘rows.length’ like below:

 //Log the number of records returned by the query
var gr = new GlideRecord('incident');
gr.addQuery('category', 'software');
gr.query();
alert('Incident count: ' + gr.rows.length);

setLimit

Used to limit the number of results returned

 //Find the last 10 incidents created
var gr = new GlideRecord('incident');
gr.orderByDesc('sys_created_on');
gr.setLimit(10);
gr.query();

chooseWindow

ChooseWindow will return all records between the first parameter(inclusive) and the second parameter(exclusive), so this example will return the 10 incidents between record 10-19 both inclusive. Works with orderBy

 //Limit the query to 10 incidents
var gr = new GlideRecord('incident');
gr.chooseWindow(10, 20);
gr.query();


setWorkflow

Used to enable or disable the triggering of Business Rules

 var gr = new GlideRecord('incident');
gr.addNullQuery('short_description');
gr.query();
while(gr.next()){
	gr.short_description = 'Must be the printers that are broken again...';
	gr.setWorkflow(false); //Disable business rules for this query
	gr.update();
}

autoSysFields

Used to prevent updating fields such as sys_updated_on, sys_updated_by, and other 'sys' fields

 //Change the category of all 'software' incidents to 'hardware' without updating sys fields
var gr = new GlideRecord('incident');
gr.addQuery('category', 'software');
gr.query();
while(gr.next()){
gr.category = 'hardware';
gr.autoSysFields(false);
gr.update();
}

setForceUpdate

Used to force an update, even when no fields are changed, and thus force the update to execute

 var gr = new GlideRecord('incident');
gr.query();
while(gr.next()) {
	gr.setForceUpdate(true); // Force an update even with no changes
	gr.update();
}

setAbortAction

Sets the next database operation to be aborted. Useful to prevent updates if specific criteria is met.

 //Standard date comparison of a start date that must be set before an end date
if ((!current.u_date1.nil()) && (!current.u_date2.nil())) {
	var start = current.u_date1.getGlideObject().getNumericValue();
	var end = current.u_date2.getGlideObject().getNumericValue();
	if (start > end) {
		gs.addInfoMessage('Start must be before end');
		current.u_date1.setError('Start must be before end');
		current.setAbortAction(true);
	}
}

List of Operators

These operators can be used in addition to the standard field/value query searching shown above…

 // =
//Equals (this is the same as not including the 3rd parameter)
addQuery('priority', '=', 1);

// >
//Greater than
addQuery('priority', '>', 1);

// <
//Less than
addQuery('priority', '<', 1);

// >=
//Greater than or equals
addQuery('priority', '>=', 1);

// <=
//Less than or equals
addQuery('priority', '<=', 1);

// !=
//Not equals
addQuery('priority', '!=', 1);

// STARTSWITH
//Field must start with value
addQuery('short_description', 'STARTSWITH', 'Printer');

// ENDSWITH
//Field must end with value
addQuery('short_description', 'ENDSWITH', 'Printer');

// CONTAINS
//Field must contain value somewhere
addQuery('short_description', 'CONTAINS', 'Printer');

// DOES NOT CONTAIN
//Field must not contain value anywhere
addQuery('short_description', 'DOES NOT CONTAIN', 'Printer');

// IN
//Field must be found somewhere in the value
addQuery('sys_id', 'IN', '8d641046c0a80164000bc7c0d3ed46a0,a9a16740c61122760004fe9095b7ddca');

// INSTANCEOF
//Return only records that are instances of an extended table (like incident is of task in this example)
addQuery('sys_class_name', 'INSTANCEOF', 'incident');

Thanks for using our GlideRecord Cheat Sheet!

Looking for something else? Check out our other Cheat Sheets below!

Credit: ServiceNow Guru

Snowycode team
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Read more