logo

NJP

Understanding GlideRecord: A Practical Quick Guide for ServiceNow Developers

New article articles in ServiceNow Community · Oct 17, 2025 · article

Hello ServiceNow Family,

This article is written for anyone who’s ready to move beyond ServiceNow administration and step into development. If you’ve been managing forms, catalogs, and configurations, and you’re now curious about what happens behind the scenes - this is for you.

It’s also useful for developers who already write scripts but want a deeper, more structured understanding of GlideRecord. Whether you’re preparing for an interview, sharpening your scripting skills, or looking to automate more of your daily work, you’ll find something valuable here.

So lets Start.................................... 🙂

Author: Ravi Gaurav

Introduction

GlideRecord is one of the most powerful APIs in ServiceNow. It allows you to interact with any table in the platform using server-side JavaScript. You can query, insert, update, and delete records, just like you would with SQL.

This document summarizes the most common GlideRecord operations, with examples and practical tips for real-world development.

1. Querying Records

The most common use case is reading data from a table.

You create a GlideRecord object, define your query, and loop through the results.

// Create a GlideRecord object for the 'incident' table

var gr = new GlideRecord('incident');

// Add a query condition: only get incidents where priority = 1

gr.addQuery('priority', 1);

// Run the query

gr.query();

// Loop through each matching record

while (gr.next()) {

// Print the incident number and short description

gs.info(gr.number + ' - ' + gr.short_description);

}

This script fetches all Priority 1 incidents and prints their number and short description.

2. Multiple Conditions

You can add multiple filters to narrow your search.

var gr = new GlideRecord('incident');

``

// Add a condition: priority less than or equal to 3

gr.addQuery('priority', '<=', 3);

``

// Add another condition: only active incidents

gr.addQuery('active', true);

``

// Execute the query

gr.query();

``

while (gr.next()) {

gs.info(gr.number); // Display incident number

}

``

This fetches all active incidents where the priority is 1, 2, or 3.

3. OR Conditions

Sometimes you need results that meet one condition or another. Use addOrCondition().

``

var gr = new GlideRecord('incident');

``

// Create the first condition

var qc = gr.addQuery('priority', 1);

``

// Add OR condition: priority = 2

qc.addOrCondition('priority', 2);

``

// Run the query

gr.query();

``

This returns all incidents where the priority is 1 or 2.

4. Insert a Record

You can create a new record programmatically using insert().

``

var gr = new GlideRecord('incident');

``

// Prepare a new empty record

gr.initialize();

``

// Set field values

gr.short_description = 'New record created via script';

gr.priority = 2;

``

// Insert the record and get its sys_id

var sys_id = gr.insert();

``

// Print the sys_id of the newly created record

gs.info('Created record: ' + sys_id);

``

Tip: Always use initialize() before insert() to ensure you’re creating a clean new record.

5. Update a Record

Use get() or query() to find a record, then modify fields and call update().

``

var gr = new GlideRecord('incident');

``

// Find a record where the number is 'INC0010001'

if (gr.get('number', 'INC0010001')) {

``

// Change the short description

gr.short_description = 'Updated via script';

``

// Save the changes

gr.update();

}

``

This updates the short description of a specific incident.

6. Delete a Record

Use deleteRecord() carefully — it permanently removes the record.

``

var gr = new GlideRecord('incident');

``

// Find the record with the given number

if (gr.get('number', 'INC0010002')) {

``

// Delete that record

gr.deleteRecord();

}

``

Best Practice: Never delete in bulk unless you’re 100% sure of the filter.

7. Encoded Queries

Encoded queries let you combine conditions in a compact way.

``

var gr = new GlideRecord('incident');

``

// Add multiple conditions using encoded query syntax

gr.addEncodedQuery('active=truepriority=1ORpriority=2');

``

// Run the query

gr.query();

``

This retrieves all active incidents that have priority 1 or 2.

Tip: You can copy encoded queries directly from the list view filter in the platform.

8. Sorting Results

Use orderBy() or orderByDesc() to control the order of your results.

``

var gr = new GlideRecord('incident');

``

// Sort records in descending order of the 'opened_at' field

gr.orderByDesc('opened_at');

``

// Run the query

gr.query();

``

while (gr.next()) {

gs.info(gr.number + ' - ' + gr.opened_at);

}

``

This shows the most recently opened incidents first.

9. Limiting Results

If you only need a few records, use setLimit() to improve performance.

``

var gr = new GlideRecord('incident');

``

// Limit results to 5 records

gr.setLimit(5);

``

// Run the query

gr.query();

``

while (gr.next()) {

gs.info(gr.number);

}

``

10. Checking if Records Exist

Before looping, you can check if any records match your query using hasNext().

``

var gr = new GlideRecord('incident');

gr.addQuery('priority', 1);

gr.query();

``

if (gr.hasNext()) {

gs.info('High-priority incidents found.');

} else {

gs.info('No high-priority incidents.');

}

``

11. Null and Not Null Queries

Use these when you need to find empty or filled fields.

``

// Find incidents where 'assigned_to' field is empty

var gr = new GlideRecord('incident');

gr.addNullQuery('assigned_to');

gr.query();

``

while (gr.next()) {

gs.info('Unassigned Incident: ' + gr.number);

}

``

// Find incidents where 'assigned_to' has a value

var gr2 = new GlideRecord('incident');

gr2.addNotNullQuery('assigned_to');

gr2.query();

``

12. Controlling Workflows and System Fields

When running data migrations or background jobs, you may not want workflows or system updates to trigger.

``

var gr = new GlideRecord('incident');

gr.initialize();

``

// Turn off workflows and system field updates

gr.setWorkflow(false); // Stops business rules and flows

gr.autoSysFields(false); // Prevents auto-updating sys fields

``

gr.short_description = 'Inserted without workflow';

gr.insert();

``

13. Using GlideAggregate for Calculations

GlideAggregate helps you perform grouped calculations like COUNT, SUM, or AVG.

``

var ga = new GlideAggregate('incident');

``

// Count the number of incidents grouped by priority

ga.addAggregate('COUNT');

ga.groupBy('priority');

ga.query();

``

while (ga.next()) {

var count = ga.getAggregate('COUNT');

gs.info('Priority: ' + ga.priority + ' | Count: ' + count);

}

``

14. Advanced Filtering Example

Combine filters, date ranges, and sorting for more control.

``

var gr = new GlideRecord('incident');

``

// Exclude closed incidents

gr.addQuery('state', '!=', 7);

``

// Only include incidents opened in the last 30 days

gr.addQuery('opened_at', '>=', gs.daysAgoStart(30));

``

// Sort by priority

gr.orderBy('priority');

``

// Execute the query

gr.query();

``

while (gr.next()) {

gs.info(gr.number + ' - ' + gr.short_description);

}

``

Explanation:

gs.daysAgoStart(30) gives the date from 30 days ago.

This query lists all open incidents created within the last month.

15. Best Practices

  • Always filter your queries to avoid performance issues.
  • Avoid using query() without conditions on large tables.
  • Use encoded queries for readability.
  • Use setLimit() when testing scripts.
  • Disable workflows carefully when running data jobs.
  • Avoid using update() or deleteRecord() inside loops unless absolutely necessary.

16. Common Mistakes to Avoid

  1. Forgetting to call query() before looping.
  2. Running scripts on the wrong table.
  3. Using gr.get() on multiple records (it only returns one).
  4. Forgetting to escape quotes in string values.
  5. Missing parentheses in conditions when using complex logic.

17. Debugging Tips

  • Use gs.info() for output in background scripts.
  • Use gs.log() for custom log messages with source tags.
  • Check system logs in System Logs > All.
  • Use gr.getTableName() to confirm the table you’re querying.

Conclusion

GlideRecord is at the heart of most ServiceNow server-side development. Whether you are automating a business rule, writing a script include, or troubleshooting data, understanding these queries will make your work faster and cleaner.

A solid grasp of GlideRecord ensures you write efficient, secure, and maintainable scripts that perform well at scale.

Warm Regards,

Ravi Gaurav

ServiceNow MVP 2025 | 2024

Associate Consultant | Solution Architect

MTECH in DataScience & AI

Social Links:

🌐 LinkedIn

🎥 YouTube

Empowering business through scalable and secure ServiceNow solutions.”

View original source

https://www.servicenow.com/community/developer-articles/understanding-gliderecord-a-practical-quick-guide-for-servicenow/ta-p/3407399