Skip to main content

ServiceNow GlideRecord API & Methods

· 3 min read
Kaustubh Kulkarni

The GlideRecord API is a powerful and versatile API in ServiceNow that enables developers to query, update, and manipulate records in tables. With the GlideRecord API, developers can perform a wide range of functions, such as inserting records, updating records, deleting records, and querying records.

In this section, we'll explore the GlideRecord API and its various methods, along with examples of how to use them.

Creating a GlideRecord Object To get started with the GlideRecord API, we first need to create a GlideRecord object that represents the table we want to query or manipulate. The constructor of the GlideRecord object takes the name of the table as an argument.

// Create a GlideRecord object for the Incident table
var gr = new GlideRecord('incident');

Once we have the GlideRecord object, we can perform a variety of operations on it using the available methods.

Querying Records The GlideRecord API provides several methods for querying records in a table, including addQuery(), addEncodedQuery(), and query(). These methods allow you to filter the records in the table based on certain conditions.

// Query the Incident table for all records with a priority of 1
var gr = new GlideRecord('incident');
gr.addQuery('priority', '1');
gr.query();
while (gr.next()) {
gs.log(gr.number);
}

In this example, we're querying the Incident table for all records with a priority of 1. We use the addQuery() method to specify the query condition and then call the query() method to execute the query. Finally, we loop through the result set using the next() method and log the incident number for each record.

Updating Records The GlideRecord API provides several methods for updating records in a table, including setValue(), setDisplayValue(), and update(). These methods allow you to modify the values of one or more fields in a record.

// Update the Short Description of a record in the Incident table
var gr = new GlideRecord('incident');
gr.get('number', 'INC0010001');
gr.setValue('short_description', 'New Short Description');
gr.update();

In this example, we're updating the Short Description of a record in the Incident table with number INC0010001. We use the get() method to retrieve the record with the specified number, and then use the setValue() method to set the new value for the Short Description field. Finally, we call the update() method to save the changes.

Deleting Records The GlideRecord API provides the deleteRecord() method for deleting records from a table.

// Delete a record from the Incident table
var gr = new GlideRecord('incident');
gr.get('number', 'INC0010001');
gr.deleteRecord();

In this example, we're deleting a record from the Incident table with number INC0010001. We use the get() method to retrieve the record with the specified number and then call the deleteRecord() method to delete it.

Working with Multiple Tables The GlideRecord API also supports working with multiple tables. You can use the addJoinQuery() method to join two tables and then query records based on the fields in both tables.

// Join the Incident and User tables and query records based on the caller's name
var gr = new GlideRecord('incident');
gr.addJoinQuery('caller_id', 'sys_user', 'name');
gr.addQuery('sys_user.name', 'John Smith');
gr.query();
while (gr.next()) {
gs.log(gr.number);
}

In this example, we're joining the Incident and User tables on the caller_id field and the sys_user table's name field. We then use the addQuery() method to filter