What It Does
The getRowCount() method executes the current query and returns the total number of records in the result set as an integer. Unlike methods that return data from individual records, this method processes the entire query result to provide an accurate count.
Internally, ServiceNow executes the full database query including all addQuery() conditions, addEncodedQuery() filters, and ACL restrictions. The platform retrieves all matching records and loads them into the GlideRecord object, then counts the total. This means the method has the same performance characteristics as a full query() operation.
The method returns 0 when no records match the query conditions, and never returns null or undefined. The count respects all security restrictions—records that the current user cannot read due to ACLs are excluded from the total.
Edge cases include queries on tables with large datasets where getRowCount() may hit memory limits or timeout. The method also includes records that might be filtered out by client-side display business rules, since it operates at the database level before display processing occurs.
This method complements hasNext() and next() by providing the total count without requiring iteration through individual records. Unlike getDisplayValue() or getValue(), it doesn't require the cursor to be positioned on a specific record.
When to Use This
Use getRowCount() when you need the exact number of records for business logic decisions, pagination calculations, or progress indicators where you're already working with a GlideRecord object. It's most appropriate when you'll also be processing the actual record data, since the query execution cost is already incurred.
For count-only operations, use GlideAggregate instead—it performs database-level counting without loading records into memory. When you only need to check if records exist, use hasNext() after query() rather than checking if getRowCount() > 0.
Avoid using getRowCount() in loops or frequently-called functions with large datasets—each call executes a full database query.
Return Value
Returns a JavaScript number representing the count of records in the result set. The value is always a non-negative integer, with 0 indicating no matching records were found. The count includes only records that the current user has read access to based on ACL evaluation.
The method never fails or throws exceptions—even malformed queries return 0. You can safely use the return value in mathematical operations, comparisons, or string concatenation without null checking.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.
Platform Behavior & Side Effects
- Executes a full database query with identical performance impact to
query()—loads all matching records into memory - Triggers ACL evaluation on every record in the result set, excluding inaccessible records from the count
- Does not fire Business Rules, notifications, or audit records—it's a read-only operation
- Results are not cached—each call executes a fresh database query
- Can hit transaction timeout limits on tables with millions of records or complex query conditions
- Behaves identically in before/after Business Rules and Script Includes—no execution context differences
- Memory consumption scales with result set size—large counts can impact available heap space