Aircraft by Modelmodel-aircrafts.com

Glide Record Example: How to Use GlideRecord in ServiceNow

The direct answer is that a GlideRecord example in ServiceNow involves instantiating the object with a table name, applying filters, executing the query, and then looping through or modifying the results. You use it because it provides a standardized, server-side way to interact with the database without writing raw SQL.

This approach ensures data integrity and leverages the platform’s built-in security checks. The core workflow follows a strict sequence: declare the record, set your conditions, run the query, and handle the data.

The Basic Query Structure

To retrieve data, you must first create an instance of the GlideRecord class. This instance acts as a pointer to a specific table in the ServiceNow database. The most common table used for examples is the Incident table, often referenced by its system ID or table name.

You begin by declaring the variable. Then, you call the initialize() method to prepare the object. After that, you apply any necessary filters using the addQuery() method. Finally, you execute the query with query(). If you need to iterate over the results, you wrap your logic in a while(next()) loop.

Here is a standard pattern for reading records:

var gr = new GlideRecord('incident');
gr.addQuery('priority', 1); // High priority
gr.addQuery('state', 2);    // In Progress
gr.query();
while (gr.next()) {
    gs.info(gr.number + ': ' + gr.short_description);
}

This code finds all high-priority incidents that are currently in progress. It prints the incident number and short description to the system log. You can replace the table name and query conditions to target any other list in the application.

Creating New Records

Creating a record requires a slightly different flow than reading one. Instead of just filtering, you must explicitly tell the system to insert a new row into the database. This is done by calling the insert() method on the GlideRecord object.

Before inserting, you should populate the fields you want to set. You can do this by assigning values directly to the field names on the object. Once all desired fields are set, you call insert(). This method returns the sys_id of the newly created record, which you can store if needed for later reference.

A typical creation script looks like this:

var newIncident = new GlideRecord('incident');
newIncident.initialize();
newIncident.short_description = 'Test record from script';
newIncident.priority = 3; // Medium
newIncident.category = 'software';
newIncident.insert();
gs.info('Created record: ' + newIncident.sys_id); 

Notice the use of initialize() before setting values. This ensures the object has the correct structure for the target table. Skipping this step can lead to errors or unexpected behavior when the system tries to map your data to the schema.

Updating Existing Data

Updating records follows the same initial steps as querying. You declare the GlideRecord, add your filter to find the specific rows you want to change, and run the query. However, instead of just reading the data, you modify the field values and then call the update() method.

The update() method saves changes only for the current record in the loop. If you are iterating through multiple records, you must call update() inside the loop for each item you wish to save. Forgetting this step means your changes will be lost when the script finishes.

Here is how you might update a status:

var gr = new GlideRecord('incident');
gr.addQuery('caller_id', someUserId);
gr.addQuery('state', 1); // New
gr.query();
while (gr.next()) {
    gr.state = 2; // Change to In Progress
    gr.update();
}

This script finds all new incidents assigned to a specific user and moves them to the in-progress state. Always ensure your query is precise enough to target only the records you intend to modify. Broad queries can accidentally update large volumes of data.

Deleting Records Safely

Deletion is the most dangerous operation you can perform with GlideRecord. Once a record is deleted, it is gone unless you have a backup. To delete, you follow the standard query process, but instead of update(), you call deleteRecord().

Like updating, deletion happens per record within the loop. You should always test your query conditions thoroughly before adding the delete command. A common practice is to comment out the delete line while you verify that the query returns exactly the records you expect.

var gr = new GlideRecord('incident');
gr.addQuery('short_description', 'Old Test Data');
gr.query();
while (gr.next()) {
    gr.deleteRecord();
}

This removes any incidents with the specified short description. Be cautious with wildcards or broad filters. Deleting the wrong data can disrupt workflows and require manual restoration from backups.

Performance and Best Practices

Using GlideRecord efficiently is critical for maintaining system performance. Every query consumes resources, so you should avoid unnecessary loops or redundant database calls. Always limit your result sets by adding appropriate filters.

If you only need a count of records rather than the data itself, use the getCount() method. This is much faster than looping through every record just to tally them. Similarly, if you need to check if a record exists, you can use isValidRecord() after a query to verify the result.

Avoid hard-coding table names or field names whenever possible. Using variables or configuration tables makes your scripts more flexible and easier to maintain. Also, remember that GlideRecord operates server-side. You cannot use it directly in client-side scripts that run in the browser.

Always respect the platform’s security model. GlideRecord automatically applies access control lists, meaning a user running the script can only see records they have permission to view. This prevents unauthorized data exposure but may cause your scripts to return fewer results than expected if permissions are restricted.

Related

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