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?
- Basic Query
- Get a Specific Record
- Multiple Conditions
- OR Conditions
- Encoded Queries
- Limit Results
- Sort Results
- Count Records
- Insert a Record
- Update a Record
- Delete a Record
- Get Field Values
- Reference Fields
- GlideRecord Method Reference
- Common GlideRecord Gotchas
- Before Running a Script
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:
var gr = new GlideRecord('TABLE_NAME');gr.addQuery('FIELD_NAME', 'VALUE');gr.query();while (gr.next()) { // Work with the record}
The basic process is:
- Create the GlideRecord object.
- Add your query conditions.
- Execute the query with
query(). - 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().
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
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.
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.
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.
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.
active = trueANDpriority = 1
Using a Reference Field
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.
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:
priority = 1ORpriority = 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.
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.
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
var inc = new GlideRecord('incident');inc.orderBy('number');inc.query();while (inc.next()) { gs.info(inc.number);}
Descending Order
var inc = new GlideRecord('incident');inc.orderByDesc('sys_created_on');inc.query();while (inc.next()) { gs.info(inc.number);}
Newest Records First
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.
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().
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().
var inc = new GlideRecord('incident');if (inc.get('SYS_ID_HERE')) { inc.short_description = 'Updated by GlideRecord'; inc.update();}
Update Multiple Records
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
var inc = new GlideRecord('incident');if (inc.get('SYS_ID_HERE')) { inc.deleteRecord();}
Delete Multiple Records
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
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.
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.
var caller = inc.getDisplayValue('caller_id');gs.info(caller);
Get the sys_id
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.
var callerSysId = inc.getValue('caller_id');var callerName = inc.getDisplayValue('caller_id');
GlideRecord Method Reference
| Method | Purpose |
|---|---|
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.
gr.addQuery('active', true);gr.query();
2. Don’t Forget next()
gr.query();while (gr.next()) { // Work with the current record}
3. Be Careful With Bulk Updates
while (gr.next()) { gr.active = false; gr.update();}
Always verify the query before running a bulk update in production.
4. Be Careful With deleteMultiple()
gr.deleteMultiple();
The query determines which records are deleted. A mistake in the query can have serious consequences.
5. Understand Reference Fields
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
| Task | Typical Pattern |
|---|---|
| Query | addQuery() → query() → next() |
| Get a record | get(sys_id) |
| Insert | initialize() → insert() |
| Update | get() → change fields → update() |
| Delete one | get() → deleteRecord() |
| Delete many | query() → deleteMultiple() |
| Limit results | setLimit() |
| Sort ascending | orderBy() |
| Sort descending | orderByDesc() |
| Get stored value | getValue() |
| Get display value | getDisplayValue() |
| Get sys_id | getUniqueValue() |
Final GlideRecord Template
If you only remember one pattern, remember this:
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:
- Create the GlideRecord.
- Build the query.
- Execute the query.
- 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:

Leave a Reply