How To Get The Number Of Records/Rows In A GlideRecord Query With getRowCount()

There are many times in a ServiceNow Developer’s life where they simply need to know how many records are being returned in a GlideRecord query. Are we about to execute code against a 100 incidents, …

servicenow gr.getRowCount()

There are many times in a ServiceNow Developer’s life where they simply need to know how many records are being returned in a GlideRecord query.

Are we about to execute code against a 100 incidents, a 1,000 incidents or are there even any records at all that meet our conditions?

A very simple GlideRecord method comes into play here.

After a GlideRecord query is created, we can simple branch off of that object and get the number of records.

How To Get The GlideRecord Row Count

You first need to have a GlideRecord object.

Then you need to execute the .query() method.

After that, you simply need to use .getRowCount(), which will always return a number.

If the result of using object.getRowCount() is 0, you either have a bad GlideRecord query, or there truly are 0 records for the query you’ve built out.

If you did build out the query incorrectly, ServiceNow will throw a warning or error log in the syslog table, letting you know this.

So if you were to run the below, in a background script, for example, the variable rowCount (you can call this anything), will return the number of rows where the priority of the incident is one.

Remember that you don’t even have to do this in a loop, you can perform this action before you even execute the if statement/while loop.

You can use this in an unlimited number of ways.

You could run this in a background script to cleanup data, you could run this in a scheduled job to make sure that certain data does or does not exist, etc.

If you just need the number of rows returned, you don’t even need the while loop in the example below.

Here’s how you get the row count of any GlideRecord query in ServiceNow:

var gr = new GlideRecord("incident");
gr.addQuery("priority", 1);
gr.query();

// Returns the number of records found, in the query
var rowCount = gr.getRowCount();

while (gr.next()) {
    // do something
}

Alternatively, you can use ServiceNow GlideAggregate class, to retrieve row count, and a ton more helpful data about the table of data that you’re working with.

ServiceNow actually recommends that you use GlideAggregate wherever you can, over getRowCount(). While GlideAggregate is probably a more performant operation, using .getRowCount() is totally fine.

You’re probably talking about milliseconds to hundreds of milliseconds of time saving between the 2. The minuscule time savings could be tested, but the 2 can really be used synonymously in our opinion. If you’ve got tables with hundreds of thousands or millions of records, you might want to consider using GlideAggregate to get a row count.

We’ll cover GlideAggregate in a different post soon, as it’s complex enough that it deserves it’s own dedicated article.

Can you build upon this logic at all?

What are use cases that you’ve come across where this was helpful?

Let us know if you have any questions below.



What Do You Think About This Article?

5 1 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mikael
Mikael
1 year ago

Thanks for the article!

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