What It Does

The next() method moves the internal cursor of a GlideRecord object to the next record in the result set returned by a query. When you execute a query with query(), ServiceNow doesn't load all records into memory at once. Instead, it maintains a cursor that points to the current record position in the result set.

Internally, ServiceNow fetches records from the database in batches (typically 100-250 records depending on the table and system configuration). When you call next(), the platform either advances to the next record in the current batch or triggers a new database query to fetch the next batch if needed. This lazy-loading approach keeps memory usage manageable even for large result sets.

The method returns true if it successfully moves to a record, false when there are no more records to process. When next() returns true, all field values on the GlideRecord object are automatically populated with data from the current record, allowing you to access fields using standard dot notation.

Edge cases include calling next() without first executing query() (which returns false), calling it on a GlideRecord with no query conditions (which iterates through all accessible records in the table), and calling it after the result set is exhausted (which continues to return false).

The next() method is closely related to hasNext() which checks if more records exist without advancing the cursor, and getRowCount() which returns the total number of records in the result set. Unlike database cursors in other platforms, you cannot move backwards or jump to arbitrary positions—GlideRecord iteration is strictly forward-only.

When to Use This

Use next() when you need to process multiple records returned by a query, perform calculations across a result set, or build arrays or objects from database records. This is the correct approach for any scenario where you expect more than one record and need to examine each one. The while(gr.next()) pattern is so fundamental that experienced ServiceNow developers write it without thinking.

Don't use next() when you only need to check if records exist (use hasNext() instead), when you need exactly one record (use get() with a sys_id or unique field), or when you're working with the current record in a Business Rule (the current object is already positioned at the right record). A common mistake is using next() in a loop when you should be using deleteMultiple() or updateMultiple() for bulk operations.

Return Value

Returns a boolean value: true when the cursor successfully advances to a record, false when no more records are available. When next() returns false, the GlideRecord object's fields become undefined and accessing them will return empty values.

The return value is safe to use directly in conditional statements without additional null checks. ServiceNow guarantees that next() will always return a proper boolean, never null or undefined. This makes it ideal for use in while loops and if conditions.

Free Newsletter

Enjoying this? Get one deep-dive per week.

Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.

No spam · Unsubscribe anytime

Platform Behavior & Side Effects

  • No Business Rules fire when calling next()—it only reads data and positions the cursor
  • ACL read permissions are enforced—records the user can't read are silently skipped from the result set
  • Database queries are executed in batches to optimize memory usage, with additional queries triggered automatically as needed
  • Performance is generally fast for indexed queries but can be slow for table scans on large tables without proper addQuery() conditions
  • No database writes occur—this is a read-only operation that doesn't affect audit trails or update timestamps
  • Field values are populated using the current user's session timezone and locale settings for date/time and number formatting
  • Query result sets are not cached—running the same query again will execute fresh database calls