Understanding GlideRecord Queries In ServiceNow

What Is A GlideRecord? The GlideRecord class is one of the most important concepts to grasp when learning ServiceNow administration and development. You will see GlideRecord queries everywhere in the system, and not properly understanding …


Buy The "ServiceNow Developer's Manual" Now

We've packed over a decade of ServiceNow Development advice, into a single e-book.

Buy It Now

What Is A GlideRecord?

The GlideRecord class is one of the most important concepts to grasp when learning ServiceNow administration and development.

You will see GlideRecord queries everywhere in the system, and not properly understanding it – will drastically limit your ability to excel in a ServiceNow career.

In ServiceNow, a GlideRecord is a class used to query, update, and insert records in a ServiceNow table. It is a way to access data from the ServiceNow platform database. A GlideRecord object holds a database record and can be used to manipulate records in the database.

The GlideRecord class should only ever be used in server-side scripting, so business rules, script includes, etc. Never use these in client side queries.

The GlideRecord method takes one single parameter in, which is the ServiceNow table, in a string form. For example, “incident”, “problem”, “change_request”, are all examples of parameters accepted for the GlideRecord class.

Here is an example of how you might use a GlideRecord to retrieve a list of incidents from the database:

var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.query();
while (gr.next()) {
   gs.print(gr.number + ' ' + gr.short_description);
}

This example creates a new GlideRecord object for the incident table, adds a query to only retrieve active incidents, and then retrieves the matching records. It then iterates through the returned incidents, printing the incident number and short description for each one.

Common GlideRecord Methods

Let’s look at some more common GlideRecord methods.

You’re going to see the following methods very frequently when you’re building out your queries.

Here is a table with some of the most common GlideRecord methods in ServiceNow:

Method nameWhat it does
addQueryAdds a query to filter the records that are returned by the GlideRecord
queryRetrieves the records that match the current query
nextRetrieves the next record in the GlideRecord set
getRetrieves the value of a field in the current record
updateSaves the current record and updates it in the database
insertInserts a new record into the database
deleteRecordDeletes the current record from the database
addEncodedQueryAdds an encoded query to the GlideRecord, which allows for more complex queries using encoded parameters

The idea with GlideRecord queries essentially is to decide on a table, build out a query, and then loop through a subset of records from the returned data set.

Let’s see how some of these common methods are used in a couple examples below, when we query the incident table.

An Example GlideRecord Query

Every ServiceNow instance has an incident table, so let’s use that.

Let’s say that we want to return all records where a certain condition is true.

Here is an example of how you might use a GlideRecord to query the incident table and find all P1 incidents that are over 15 days old and are still active:

var gr = new GlideRecord('incident');
gr.addQuery('priority', '1');
gr.addQuery('active', true);
gr.addQuery('sys_created_on', '<', gs.daysAgo(15));
gr.query();
while (gr.next()) {
   gs.print(gr.number + ' ' + gr.short_description);
}

This example creates a new GlideRecord object for the incident table, adds three queries to filter the records that are returned: one to only retrieve P1 incidents, one to only retrieve active incidents, and one to only retrieve incidents that were created more than 15 days ago. It then retrieves the matching records and iterates through them, printing the incident number and short description for each one.

This GlideRecord will return all P1 incidents that are still active and were created more than 15 days ago.

But in this example above, we’re just finding these incidents, printing out a number and looping through them.

That’s not super helpful.

Let’s actually modify the field values of these incidents.

So we’re taking a look at a table, finding certain incidents that match a condition and then we’re doing something to them.

Let’s go ahead and add a work note to all of these incidents, and then close them out, which would be a real world use case.

So in our while loop, this is where we have access to each record.

After we hit the query(), we then loop or iterate through the returned data set – one incident at a time.

To go ahead and add a work note and then close each of these incidents out, the code would look like this:

var gr = new GlideRecord('incident');
gr.addQuery('priority', '1');
gr.addQuery('active', true);
gr.addQuery('sys_created_on', '<', gs.daysAgo(15));
gr.query();
while (gr.next()) {
   gr.work_notes = "Closing P1 because it's been open for longer than 15 days";
   gr.active = false; // deactivate the incident
   gr.state = 7; // close the incident
   gr.update(); // updates the incident, with the above field changes
}

So all of our updates are occurring, one at a time, as we loop through the returned data set.

We are only updating records that return TRUE, for all of the conditions in the addQuery() statements.

Tips And Tricks When Writing GlideRecord Queries

We’ve covered some of the more useful GlideRecord methods below.

Where we’ve created articles on them, we’ve provided links to them.

Here are a few tips and tricks to keep in mind when building out GlideRecord queries in ServiceNow:

  1. Use the addQuery method to filter the records that are returned by the GlideRecord. This method allows you to specify a field, an operator, and a value to filter the records by.
    We’ve covered how to really understand addQuery() here.
  2. Use the orderBy method to specify how the records should be sorted. This method takes a field name as a parameter, and you can use the orderByDesc method to sort the records in descending order.
  3. Use the setLimit method to limit the number of records returned by the GlideRecord. This can be useful if you only need a certain number of records, or if you want to improve performance by reducing the amount of data that is returned.
  4. Use the addJoinQuery method to retrieve records from related tables. This method allows you to specify a field on the current table, an operator, and the value to filter by, and it will return records from the related table that match the specified criteria. This one is a little more complex.
  5. Use the addEncodedQuery method to build more complex queries using encoded parameters. This method allows you to specify an encoded query string that can include multiple filters, sorts, and other parameters, making it easier to build complex queries.
    I use addEncodedQuery() all the time, learn how to build out super quick and efficient addEncodedQuery() methods here.
  6. Use the getRowCount method to determine the number of records in the GlideRecord set. This can be useful if you want to know how many records were returned by the query, or if you want to implement pagination.
    Take a look at our post on getRowCount() here.

I hope these tips and tricks are helpful!

Understanding GlideRecord queries is absolutely crucial to any ServiceNow admin or developer.

If you have any further questions about building GlideRecord queries in ServiceNow, don’t hesitate to ask.



What Do You Think About This Article?

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] Go here if you want to better understand GlideRecord queries in ServiceNow, […]

1
0
Would love your thoughts, please comment.x
()
x