What It Does
The hasNext() method peeks ahead in your query results to determine if more records exist beyond the current cursor position. Unlike next(), it doesn't advance the cursor or make the next record available for processing. The cursor remains exactly where it was before the call.
Internally, ServiceNow maintains a result set cursor that tracks your position within the returned records. When you call hasNext(), the platform checks if the cursor position plus one contains a valid record. This operation doesn't trigger database queries if the results are already cached, but may cause additional database hits if the result set hasn't been fully loaded.
The method returns true if at least one more record exists after the current position, false if you're at the end of the result set. Before any calls to next(), the cursor position is before the first record, so hasNext() will return true if any records match your query.
Edge cases include scenarios where record permissions change between the hasNext() call and subsequent next() calls. If ACLs deny access to the next record after hasNext() returns true, the following next() call may skip that record and advance to the next accessible one.
Relationship to other GlideRecord methods: hasNext() complements next() for iteration control, but next() already returns false when no more records exist. The getRowCount() method provides the total number of records, while hasNext() only tells you about immediate next record availability.
When to Use This
Use hasNext() only when you need to perform different logic based on whether more records exist before processing them. This includes scenarios like building comma-separated lists where you need to know if you should add a delimiter, or when implementing custom pagination logic where you display different UI elements based on result availability. The method is also useful when you need to conditionally execute expensive operations only if more data exists.
For standard record iteration, use next() directly in your while loop condition instead of checking hasNext() first. The pattern while (gr.hasNext()) { gr.next(); } is redundant and slower than while (gr.next()) { }. When counting records, use getRowCount() instead of iterating with hasNext().
The pattern while (gr.hasNext()) { gr.next(); } makes two method calls per iteration instead of one. This doubles your loop overhead for no benefit.
Return Value
Returns a boolean value: true if more records are available from the current cursor position, false if you've reached the end of the result set. The method never returns null or undefined - it always returns a proper boolean even when called on an uninitialized GlideRecord.
If you call hasNext() before executing query(), it returns false because no result set exists. You can safely use the return value in conditional statements without additional 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
- No Business Rules, ACL checks, or audit records are triggered by
hasNext()calls - it only checks cursor position - No database writes occur - this is a read-only operation that doesn't modify data
- Performance is fast when results are cached, but may trigger additional database queries if the result set isn't fully loaded
- Behaves identically in Business Rules, Script Includes, and Scheduled Jobs - no context-specific differences
- Result may change between calls if underlying data is modified by other transactions during iteration
- Memory usage remains constant - the method doesn't load additional records into memory
- Transaction isolation level affects whether you see records created/modified by concurrent transactions