Aircraft by Modelmodel-aircrafts.com

GlideRecord in ServiceNow: How the API Works

GlideRecord is the server-side JavaScript class in ServiceNow that you use to read, create, update, and delete records in a table. You use it inside server scripts such as business rules, script includes, and workflow activities, not in client-side scripts. The main condition is where your script runs: server-side code gets GlideRecord directly, while client-side code needs a different approach such as GlideAjax or the REST APIs.

What GlideRecord Does

A GlideRecord object represents a query against one table. You create one with the table name, add query conditions, run the query, then walk through the returned rows one at a time. Each row gives you access to its fields as properties, and you can read or set those field values directly on the record. Behind the scenes, ServiceNow turns your query into database work, so you never write SQL yourself.

The class also covers the write side. The same object that reads a record can hold new values and save them, or represent a brand-new record waiting to be inserted. Because of that, one script can look something up, change it, and save it in a single flow.

Reading Records

The usual pattern has a fixed order: create the record object, add conditions, call the query method, then loop. The loop uses a next method that moves to the following row and tells you when the set is finished. Inside the loop, you read fields by name, just like properties on any object.

var gr = new GlideRecord('incident');
gr.addQuery('priority', 'High');
gr.addQuery('active', 'true');
gr.orderBy('number');
gr.query();
while (gr.next()) {
  gs.info('Found: ' + gr.getValue('short_description'));
}

Two habits keep queries cheap. First, always add conditions so you pull only the rows you need, rather than querying the whole table and filtering in script. Second, use the field-specific get and set methods when you want values as plain strings, because reading a field as a property gives you a special field object that behaves differently when you concatenate or compare it.

Writing, Updating, and Deleting

To update, query for the record, set the fields you want to change, then call the update method. To create, build a new record object with the table name, set the required fields, and call insert. To remove records, query for them and call deleteRecord on each one. There is also a deleteMultiple method that removes everything matching the current query conditions, which is powerful and easy to misuse, so most teams reserve it for clearly scoped clean-up jobs.

var gr = new GlideRecord('incident');
gr.addQuery('state', 'Closed');
gr.query();
while (gr.next()) {
  gr.work_notes = 'Archived by cleanup job';
  gr.update();
}

One detail that surprises people: calling update inside a loop fires the business rules on each of those records, just as if a user had edited them one by one. If you are updating many rows, expect the related rules, workflows, and notifications to run for each one, and plan for the time that takes.

Useful Methods to Know

A few methods do most of the daily work. The get method fetches a single record by its unique identifier, which is handy when you already know exactly which row you want and can skip the loop. The addEncodedQuery method lets you paste a query string copied from a list filter, which is faster than rebuilding conditions by hand. The setLimit method caps how many rows come back, which protects a script from a runaway query on a large table. The orderBy and orderByDesc methods control the sequence of the results. And the getDisplayValue method returns the friendly label a user would see, rather than the raw stored value, which matters for choice and reference fields.

GlideRecord and GlideRecordSecure

A close cousin, GlideRecordSecure, applies the user’s security rules to every read, so a script running on behalf of a user only sees records that user is allowed to see. Plain GlideRecord does not apply that access check the same way. Which one to use depends on the context of your script and your instance’s security policies, so check the ServiceNow developer documentation for the current guidance before choosing. As a rule of thumb, scripts that handle user-supplied input or run with a user’s permissions should not assume unrestricted table access is acceptable.

Where GlideRecord Does Not Fit

A common misconception is that GlideRecord is the only way to touch ServiceNow data from anywhere. In fact it exists only on the server side of the platform. Client scripts, service portal widgets in the browser, and external applications cannot use it directly. Client-side code typically calls the server through GlideAjax or a scripted REST endpoint, and outside systems use the REST Table API, which exposes the same tables over HTTP. The concepts map across, though: the REST API has its own query syntax that mirrors what addQuery builds, so a query you wrote for GlideRecord can usually be translated into a REST call without much rethinking.

What to Do Next

If you are writing your first script, start with a read-only query against a small table and log the results with the info method, so you can see the loop working before you change any data. Add conditions before you add anything else, and use setLimit while experimenting. When you move on to updates, test in a sub-production instance first, because an update call in a loop changes real records immediately and there is no undo prompt. From there, the natural next steps are learning addEncodedQuery for faster filtering, GlideAggregate for counting and grouping without pulling every row, and script includes for wrapping reusable GlideRecord logic in one place.

Related

Published 1 December 2026. Found an error? Tell us.