Quick answer: GlideRecord is a Java-like class in ServiceNow used to query, update, or create records in a database table. You use it in server-side scripts to loop through results or work with a single row.
Here is a basic example that queries the Incident table and prints the number of each active record:
var gr = new GlideRecord(‘incident’);
gr.addQuery(‘active’, true);
gr.query();
while (gr.next()) {
gs.print(gr.number);
}
How a GlideRecord Query Works
The example above follows a strict order: create the object, build the query, run the query, then process the results. You must call the query method before you can access any records. The system returns a GlideRecord object that acts as a pointer to the first row. Calling the next method moves the pointer to the following row. The loop continues until there are no more rows to read.
The addQuery method filters the results. In the example, it asks for records where the active field is true. You can chain multiple queries together to narrow the results further. If you skip the addQuery step, the query returns every row in that table, which can cause performance issues in large tables.
Reading and Writing Field Values
When you have a record, you read a field value by using dot notation with the field name, such as gr.number or gr.short_description. You can use these values in logic or assign them to other variables.
To update a record, you must call the update method after you set the new values. The system does not save changes automatically. For example, to close an incident, you would set the active field to false, then call gr.update(). This writes the change to the database and triggers any usual business rules.
Be careful with the setValue method. It is the safest way to write data because it handles date formats and references correctly. Direct assignment works for simple strings, but setValue is the standard practice for scripts that need to be reliable.
Creating New Records
You use GlideRecord to create new rows as well. The method is initialize, followed by setting values, then insert. The initialize method prepares a blank record in memory. You fill in the required fields, then call insert to save it. The insert method returns the unique ID of the new record.
This process is common when a script needs to generate a task or a log entry based on an event. You must set all mandatory fields before you call insert, or the system will reject the save.
Deleting Records
Deleting a record is a permanent action. You use the deleteRecord method. You query for the specific record you want to remove, then call this method. It removes the row from the table and cannot be undone through standard interface tools.
Because this is destructive, scripts that delete records usually include strict checks. You might verify the state of the record or require an admin role before the script runs the delete. This prevents accidental data loss.
Performance and Query Limits
ServiceNow enforces a limit on how many rows a query can return at once. This protects system memory. If a query matches more rows than the limit allows, the system stops reading at the limit. Your script might miss data if you do not account for this.
To handle large sets of data, you can use the setLimit method to define a specific number of rows to return. You can also add more specific filters to reduce the result count. Always try to make your query as specific as possible. A broad query on a large table slows down the instance for everyone.
GlideRecord vs. GlideAggregate
GlideRecord is for reading and writing individual rows. If you need to count records, calculate averages, or group data, you should use GlideAggregate instead. GlideAggregate is faster for those operations because it lets the database do the math rather than pulling every row into the script engine.
If you write a GlideRecord script just to count how many records match a filter, switch to GlideAggregate. It is a common mistake that leads to slow performance. Use GlideRecord when you need to see or change the data inside the rows. Use GlideAggregate when you only need to know how many rows exist or what a total value is.