GlideRecord in ServiceNow: Complete Scripting Guide
In the previous section, we learned about the server-side Glide classes. Of all of them, GlideRecord is the one you will use the most. Almost every server-side script that reads or changes data in ServiceNow uses it.
In this, we will understand GlideRecord in ServiceNow, how to query records with addQuery() and addEncodedQuery(), how to insert, update, and delete records, how next() and hasNext() work, and how GlideRecord is different from GlideAjax.
What is GlideRecord?
GlideRecord in ServiceNow is a server-side JavaScript API class. It allows you to interact directly with database tables for CRUD operations (Create, Read, Update, and Delete) without writing raw SQL.
How to use GlideRecord in ServiceNow
GlideRecord scripts used to read records generally follow these four steps:
- Create a GlideRecord object for a table.
- Add conditions to say which records you want.
- Run the query with query().
- Loop through the results with next().
GlideRecord Query Example
The following example retrieves all priority 1 incidents and prints their incident numbers.
var gr = new GlideRecord('incident');
gr.addQuery('priority', 1);
gr.query();
while (gr.next()) {
gs.info(gr.getValue('number')); // Print each incident number
}
Here, `while (gr.next())` moves through the matching records one at a time. The script prints the number of each incident returned by the query.
GlideRecord Query Methods (addQuery, addEncodedQuery)
GlideRecord provides built-in methods in ServiceNow to query and work with database records. Methods like addQuery() and addEncodedQuery() help filter records, while query() executes the query and retrieves matching records.
They allow developers to interact with ServiceNow tables in server-side scripts (like Business Rules and Script Includes) without writing raw SQL. Both the addQuery and addEncodedQuery methods are filtering methods. Used to narrow down which records are returned before executing the search.
addQuery()
The addQuery() method adds a condition to a GlideRecord query.
Syntax:
gr.addQuery(field, operator, value);
field: The table field you want to filter.
operator: The comparison operator, such as =, !=, or CONTAINS.
value: The value you want to match.
If you do not specify an operator, the condition uses equals.
Example: Retrieve incidents with priority 1
var gr = new GlideRecord('incident');
gr.addQuery('priority', 1);
gr.query();
while (gr.next()) {
gs.info(gr.getValue('number'));
}
This query returns incidents where the priority field is set to 1.
addEncodedQuery()
The addEncodedQuery() method lets you add multiple filter conditions using one encoded query string.
You can create an encoded query from a ServiceNow list filter. Apply the required conditions to a list, right-click the filter breadcrumb, and select the option to copy the query.
Common encoded-query separators include:
^ means AND.
^OR means OR.
Example: Retrieve incidents with priority 1 or priority 2
var gr = new GlideRecord('incident');
gr.addEncodedQuery('priority=1^ORpriority=2');
gr.query();
while (gr.next()) {
gs.info(gr.getValue('number'));
}
Use addQuery() for a few straightforward conditions. Use addEncodedQuery() when you want to reuse a filter copied from a list or express several conditions in one encoded string.
addQuery vs addEncodedQuery
| addQuery() | addEncodedQuery() | |
|---|---|---|
| How it works | Adds a condition through a method call | Adds a condition through one encoded string |
| Best for | A few simple conditions | Many or complex conditions |
| Example | gr.addQuery(‘active’, true) | gr.addEncodedQuery(‘active=true^priority=1’) |
| Combining | Several calls are joined with AND | The string carries its own AND and OR |
GlideRecord CRUD Operations (insert, update, delete Record)
ServiceNow’s GlideRecord API Reference is a server-side JavaScript interface used to perform Create, Read, Update, and Delete (CRUD) operations on database tables.
CRUD stands for Create, Read, Update, and Delete. GlideRecord provides methods to perform these operations on ServiceNow table records.
A. Create a Record – insert()
To create a new record, use initialize() to prepare the GlideRecord object, set the required field values, and call insert().
Example: Create a new incident
var gr = new GlideRecord('incident');
gr.initialize();
gr.setValue('short_description', 'Printer not working');
gr.setValue('priority', 2);
var newSysId = gr.insert();
gs.info('New incident sys_id: ' + newSysId);
The `initialize()` method prepares a new record. The `insert()` method saves it to the table and returns the sys_id of the newly created record.
B. Read Records – query() and get()
You can use query() to retrieve records that match filter conditions. Use get() when you want to retrieve a specific record.
Example: Retrieve an incident using its number
var gr = new GlideRecord('incident');
if (gr.get('number', 'INC0012345')) {
gs.info(gr.getValue('short_description'));
}
The `get()` method looks for the specified record. If it finds a match, it returns `true`, and you can access the record’s field values inside the `if` block.
C. Update a Record – update()
To update a record, first retrieve it, change the required field values, and then call update() to save the changes.
Example: Update a single incident
var gr = new GlideRecord('incident');
if (gr.get('number', 'INC0012345')) {
gr.setValue('state', 2);
gr.setValue('short_description', 'Server restarted');
gr.update();
}
You can also update multiple records that match a query by using updateMultiple().
Example: Update matching incidents
var gr = new GlideRecord('incident');
gr.addQuery('state', 1);
gr.setValue('state', 2);
gr.updateMultiple();
Use `updateMultiple()` carefully. It updates every record that matches the query, so check your filter conditions before running it, especially in a production instance.
D. Delete Records – deleteRecord() and deleteMultiple()
Use deleteRecord() to delete the current record.
Example: Delete one incident
var gr = new GlideRecord('incident');
if (gr.get('number', 'INC0012345')) {
gr.deleteRecord();
}
To delete multiple records that match a condition, use deleteMultiple().
Example: Delete matching test incidents
var gr = new GlideRecord('incident');
gr.addQuery('short_description', 'CONTAINS', 'Test Cleanup');
gr.deleteMultiple();
hasNext() and next() in GlideRecord
These two methods control how you move through the records a query returns. Both next() and hasNext() help you work with records returned by a query, but they perform different tasks.
| Method | What It Does | Returns |
|---|---|---|
| next() | Moves to the next record. | true if it moved, false when there are no more records. |
| hasNext() | Checks if there are more records, without moving. | true if there are more records available |
The key difference: next() moves to the next record, while hasNext() only checks whether another record exists.
Using next()
Use next() when you want to access and process records returned by a query. It is commonly used in a while loop.
Example: Process all matching incidents
var gr = new GlideRecord('incident');
gr.query();
while (gr.next()) {
gs.info(gr.getValue('number'));
}
If you only need the first record, use if instead of while.
var gr = new GlideRecord('incident');
gr.query();
if (gr.next()) {
gs.info(gr.getValue('number'));
}
Using hasNext()
Use hasNext() when you only need to check whether the query returned another record, without moving the cursor to it.
Example: Check whether any priority 1 incidents exist
var gr = new GlideRecord('incident');
gr.addQuery('priority', 1);
gr.query();
if (gr.hasNext()) {
gs.info('At least one priority 1 incident was found.');
}
Important Notes
- Call query() before using next() or hasNext().
- Use `next()` to move to a record before accessing its field values.
- If a table has a field named next, use _next() to avoid a naming conflict. Similarly, use _query() if the table has a field named query.
GlideRecord vs GlideAjax
GlideRecord and GlideAjax are both used when working with ServiceNow data, but they have different purposes.
| Comparison | GlideRecord | GlideAjax |
|---|---|---|
| Where it is used | Server-side scripts | Client-side scripts to make server calls. |
| Common use | Business Rules, Script Includes, and Scheduled Scripts | Client Scripts and other client-side scripts |
| Main purpose | Reads and changes records in ServiceNow tables | Calls a server-side Script Include and returns data to the client |
| Database access | Provides server-side access to table records | Does not access the database directly from the client; it uses server-side logic |
| Typical example | Querying incident records in a Business Rule | Fetching information from the server without reloading the form. |
Remember: GlideRecord works with table records on the server. GlideAjax allows a client-side script to request information from the server, usually through a client-callable Script Include.
What’s Next?
In the next chapter, we will explore three useful server-side Glide classes: GlideSystem, GlideDateTime, and GlideAggregate. These classes will help you build more useful ServiceNow server-side scripts.
Next TopicNeed more support?
Get a head start with our FREE study notes!
Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.