A GlideRecord is the object ServiceNow gives you for working with records in a table through script. The main methods are the query methods that find records, the field methods that read and set values, and the write methods that insert, update, or delete. Which ones you use depends on whether you are reading data, changing it, or removing it.
What a GlideRecord Is
Think of a GlideRecord as a cursor that walks through a table one record at a time. You create it with the table name, tell it which records you want, run the query, then step through the results. At each step, the object holds one record, and you can read or change its fields directly. You can use it in business rules, script includes, scheduled jobs, and background scripts. Labels and available fields differ by instance and version, so check the table definition in your instance before you rely on a field name.
Querying Records
The pattern is always the same: create the object, add conditions, run the query, then loop. The new GlideRecord call takes the table name. The addQuery method adds a condition, such as a field equals a value, or a field is not empty. You can chain several addQuery calls and they combine with a logical and. If you need an or between conditions, you build a query condition object with addOrCondition. The query method runs everything and fetches the matching records. The next method moves the cursor forward one record and returns false when there are no more, which is why it sits inside a while loop.
You can shape the results before querying. The orderBy method sorts by a field, and orderByDesc sorts in reverse. The setLimit method caps how many records come back, which keeps a big table from slowing your script. The get method is a shortcut: it queries for a single record by its sys_id, or by a field and value pair, and positions the cursor on it. It is handy when you know exactly one record matters.
Reading and Setting Fields
Once the cursor sits on a record, getValue takes a field name and returns the value as a string. getValue returns the underlying value, while getDisplayValue returns what a user would see, which matters for reference and choice fields. For example, the assigned_to field holds a sys_id, but its display value is the assignee’s name. setValue does the opposite: it takes a field name and a value and stages the change in memory. Nothing is written to the database until you call update. You can also reach fields with dot notation, such as gr.short_description, though the method form is easier to read when the field name comes from a variable.
Writing and Deleting Records
The update method writes your staged changes and returns the sys_id of the record. It also fires the normal engine behaviour for that table, including business rules and the audit trail, so an update from script is not invisible to the rest of the platform. The insert method takes a new, empty GlideRecord, saves it, and returns the sys_id of the new record. The deleteRecord method removes the record the cursor is on. Because these fire downstream logic, test them in a sub-production instance first, and prefer setting a state or active flag over deleting when the table tracks history.
Example: Closing Stale Incidents
Here is the pattern with a realistic case. Suppose you want to close every incident in the Resolved state whose assignee is empty. You create a GlideRecord on the incident table. You add a condition that state equals Resolved, and another that assigned_to is empty. You order by the opened date so the oldest ones come first, and you set a limit so the job stays light. Then you loop with next. Inside the loop, you set close_code to a value like Solved, set the close notes to explain the automated closure, and call update. Each pass writes one record and fires that record’s rules. The exact field names and choice values depend on your instance, so confirm them in the dictionary and the choice list before you run anything against live data.
One Misconception Worth Fixing
A common belief is that getValue on a reference field returns the name of the referenced record. It does not. It returns the sys_id, which is a long internal identifier. If you want the readable name, call getDisplayValue on that field instead. Scripts that compare a reference field to a person’s name silently fail because the sys_id never matches the name. The same applies to choice fields: getValue gives the stored value, getDisplayValue gives the label shown in forms and lists.
Encrypted Fields and Performance
Two follow-up questions come up often. The first is how to read encrypted fields. You need the right context and, for some setups, a decryption method on the field object rather than a plain getValue; check the platform documentation for your encryption setup, because getting this wrong returns an unreadable value instead of an error. The second is why a query feels slow. Usually the script is looping over every matching record when it only needs a count, and the getAggregate method with a count would do the job without moving the cursor at all. Also check that the fields you filter on are indexed, since a condition on an unindexed field forces a full scan of the table.