GlideRecord Reference Sheet

The practical ServiceNow GlideRecord cheat sheet for querying, creating, updating, and deleting records with server-side JavaScript.

GlideRecord is one of the most commonly used APIs in ServiceNow development. If you work with Business Rules, Script Includes, Fix Scripts, Background Scripts, Scheduled Jobs, or other server-side scripts, you’ll use GlideRecord constantly.


Quick Navigation

What is GlideRecord?

GlideRecord is a ServiceNow server-side API used to work with records in a table.

You can use GlideRecord to:

  • Query records
  • Retrieve a specific record
  • Create records
  • Update records
  • Delete records
  • Sort query results
  • Limit the number of records returned
  • Access field values
  • Work with reference fields

The basic GlideRecord pattern looks like this:

JavaScript
var gr = new GlideRecord('TABLE_NAME');
gr.addQuery('FIELD_NAME', 'VALUE');
gr.query();
while (gr.next()) {
// Work with the record
}

The basic process is:

  1. Create the GlideRecord object.
  2. Add your query conditions.
  3. Execute the query with query().
  4. Move through the results with next().

Basic GlideRecord Query

The most common GlideRecord pattern is to query a table and loop through the results using next().

JavaScript
var inc = new GlideRecord('incident');
inc.addQuery('active', true);
inc.query();
while (inc.next()) {
gs.info('Incident: ' + inc.number);
}

In this example, ServiceNow queries the Incident table and returns active incidents.

Another Example

JavaScript
var user = new GlideRecord('sys_user');
user.addQuery('active', true);
user.query();
while (user.next()) {
gs.info(user.name);
}

Remember: addQuery() builds the query. query() executes it.

Get a Specific Record

If you already know the sys_id of a record, get() is usually the simplest way to retrieve it.

JavaScript
var inc = new GlideRecord('incident');
if (inc.get('SYS_ID_HERE')) {
gs.info(inc.number);
}

You can also use get() with a field and value.

JavaScript
var inc = new GlideRecord('incident');
if (inc.get('number', 'INC0010001')) {
gs.info(inc.short_description);
}

The if statement lets you verify that the record was actually found.

Multiple Query Conditions

You can add multiple conditions with multiple addQuery() calls.

JavaScript
var inc = new GlideRecord('incident');
inc.addQuery('active', true);
inc.addQuery('priority', 1);
inc.query();
while (inc.next()) {
gs.info(inc.number);
}

Multiple addQuery() conditions are combined using AND logic.

JavaScript
active = true
AND
priority = 1

Using a Reference Field

JavaScript
var inc = new GlideRecord('incident');
inc.addQuery('assignment_group', 'GROUP_SYS_ID');
inc.query();
while (inc.next()) {
gs.info(inc.number);
}

OR Conditions

Use addOrCondition() when you need OR logic.

JavaScript
var inc = new GlideRecord('incident');
var query = inc.addQuery('priority', 1);
query.addOrCondition('priority', 2);
inc.query();
while (inc.next()) {
gs.info(inc.number);
}

This produces logic equivalent to:

JavaScript
priority = 1
OR
priority = 2

Encoded Queries

Encoded queries are useful when working with more complicated filters.

A convenient workflow is to build your filter in a ServiceNow list view, copy the encoded query, and use it in your script.

JavaScript
var inc = new GlideRecord('incident');
inc.addEncodedQuery('active=true^priority=1');
inc.query();
while (inc.next()) {
gs.info(inc.number);
}

💡 Tip: If you’re unsure how to write a complicated query, build the filter using the ServiceNow condition builder first.

Limit Results

Use setLimit() when you only need a certain number of records.

JavaScript
var inc = new GlideRecord('incident');
inc.addQuery('active', true);
inc.setLimit(5);
inc.query();
while (inc.next()) {
gs.info(inc.number);
}

💡 Development tip: When testing a query, limiting the results can help prevent accidentally processing a huge number of records.

Sort Query Results

Use orderBy() for ascending order and orderByDesc() for descending order.

Ascending Order

JavaScript
var inc = new GlideRecord('incident');
inc.orderBy('number');
inc.query();
while (inc.next()) {
gs.info(inc.number);
}

Descending Order

JavaScript
var inc = new GlideRecord('incident');
inc.orderByDesc('sys_created_on');
inc.query();
while (inc.next()) {
gs.info(inc.number);
}

Newest Records First

JavaScript
var inc = new GlideRecord('incident');
inc.orderByDesc('sys_created_on');
inc.setLimit(10);
inc.query();
while (inc.next()) {
gs.info(inc.number);
}

Count Records

getRowCount() returns the number of records returned by the query.

JavaScript
var inc = new GlideRecord('incident');
inc.addQuery('active', true);
inc.query();
gs.info(
'Active incidents: ' + inc.getRowCount()
);

Performance note: If your only goal is to count, group, or aggregate records, consider whether GlideAggregate is a better choice.

Insert a New Record

Use initialize() to prepare a new record, set the fields you need, and then call insert().

JavaScript
var inc = new GlideRecord('incident');
inc.initialize();
inc.short_description = 'Created by GlideRecord';
inc.description =
'This incident was created by a script.';
var sysId = inc.insert();
gs.info('Created incident: ' + sysId);

The return value from insert() is the sys_id of the newly created record.

Update an Existing Record

To update a record, retrieve it, change the field you need, and call update().

JavaScript
var inc = new GlideRecord('incident');
if (inc.get('SYS_ID_HERE')) {
inc.short_description =
'Updated by GlideRecord';
inc.update();
}

Update Multiple Records

JavaScript
var inc = new GlideRecord('incident');
inc.addQuery('active', true);
inc.query();
while (inc.next()) {
inc.priority = 3;
inc.update();
}

⚠️ Be careful: A broad query combined with update() can modify a large number of records.

Delete Records

Delete One Record

JavaScript
var inc = new GlideRecord('incident');
if (inc.get('SYS_ID_HERE')) {
inc.deleteRecord();
}

Delete Multiple Records

JavaScript
var inc = new GlideRecord('incident');
inc.addQuery('active', false);
inc.query();
inc.deleteMultiple();

🚨 Warning: deleteMultiple() is a destructive operation. A mistake in the query can result in many records being deleted.

Getting Field Values

Direct Field Access

JavaScript
gs.info(inc.number);
gs.info(inc.short_description);
gs.info(inc.priority);

getValue()

Use getValue() when you want the underlying value stored in a field.

JavaScript
var number = inc.getValue('number');
var priority = inc.getValue('priority');
gs.info(number);
gs.info(priority);

getDisplayValue()

Use getDisplayValue() when you need the human-readable display value.

JavaScript
var caller =
inc.getDisplayValue('caller_id');
gs.info(caller);

Get the sys_id

JavaScript
var sysId = inc.getUniqueValue();
gs.info(sysId);

Reference Fields

Reference fields are important to understand when working with GlideRecord.

For example, an Incident’s caller_id field references a record in the User table. The stored value is generally the referenced record’s sys_id, while the display value may be the user’s name.

JavaScript
var callerSysId =
inc.getValue('caller_id');
var callerName =
inc.getDisplayValue('caller_id');

GlideRecord Method Reference

MethodPurpose
addQuery()Add a query condition.
addEncodedQuery()Add an encoded query.
addOrCondition()Add an OR condition.
query()Execute the query.
next()Move to the next returned record.
get()Retrieve a specific record.
initialize()Prepare a new record for insertion.
insert()Insert a new record.
update()Update the current record.
deleteRecord()Delete the current record.
deleteMultiple()Delete records returned by the query.
setLimit()Limit the number of returned records.
orderBy()Sort results in ascending order.
orderByDesc()Sort results in descending order.
getRowCount()Return the number of returned records.
getValue()Return a field’s underlying value.
getDisplayValue()Return a field’s display value.
getUniqueValue()Return the current record’s sys_id.

Common GlideRecord Gotchas

1. Don’t Forget query()

Adding a condition does not execute the query.

JavaScript
gr.addQuery('active', true);
gr.query();

2. Don’t Forget next()

JavaScript
gr.query();
while (gr.next()) {
// Work with the current record
}

3. Be Careful With Bulk Updates

JavaScript
while (gr.next()) {
gr.active = false;
gr.update();
}

Always verify the query before running a bulk update in production.

4. Be Careful With deleteMultiple()

JavaScript
gr.deleteMultiple();

The query determines which records are deleted. A mistake in the query can have serious consequences.

5. Understand Reference Fields

JavaScript
gr.getValue('caller_id');
gr.getDisplayValue('caller_id');

6. Consider GlideAggregate

If you’re counting, grouping, or aggregating records, GlideAggregate may be more appropriate than retrieving every record with GlideRecord.

Before You Run a GlideRecord Script

  • ☐ Confirm you’re using the correct table.
  • ☐ Verify every query condition.
  • ☐ Test the query before changing data.
  • ☐ Check how many records the query returns.
  • ☐ Use setLimit() while testing when appropriate.
  • ☐ Double-check scripts containing update().
  • ☐ Double-check scripts containing deleteRecord().
  • ☐ Be especially careful with deleteMultiple().
  • ☐ Test potentially destructive scripts in a safe environment first.

GlideRecord Quick Reference

TaskTypical Pattern
QueryaddQuery() → query() → next()
Get a recordget(sys_id)
Insertinitialize() → insert()
Updateget() → change fields → update()
Delete oneget() → deleteRecord()
Delete manyquery() → deleteMultiple()
Limit resultssetLimit()
Sort ascendingorderBy()
Sort descendingorderByDesc()
Get stored valuegetValue()
Get display valuegetDisplayValue()
Get sys_idgetUniqueValue()

Final GlideRecord Template

If you only remember one pattern, remember this:

JavaScript
var gr = new GlideRecord('TABLE_NAME');
gr.addQuery('FIELD_NAME', 'VALUE');
gr.query();
while (gr.next()) {
// Do something with the record
}

From there, you can add sorting, limits, additional conditions, encoded queries, updates, or other GlideRecord operations as needed.

Conclusion

GlideRecord is one of the most important APIs to understand if you’re developing on ServiceNow.

The core pattern is simple:

  1. Create the GlideRecord.
  2. Build the query.
  3. Execute the query.
  4. Process the records.

Once you’re comfortable with that pattern, you can build much more advanced ServiceNow scripts.

Save this page as your GlideRecord cheat sheet and come back whenever you need a quick syntax reference.


More ServiceNow tips, scripts, and reference guides:

Visit mySNstuff →

Leave a Reply

Latest Posts

Discover more from mySNstuff.com

Subscribe now to keep reading and get access to the full archive.

Continue reading