What It Does

The setLimit() method configures a GlideRecord query to return at most the specified number of records. When ServiceNow executes the subsequent query() call, it adds a SQL LIMIT clause to the underlying database query, instructing the database to stop returning results after the specified count is reached.

Internally, ServiceNow stores the limit value on the GlideRecord object and applies it during query execution. The database processes the WHERE conditions first, then applies any ORDER BY clauses, and finally limits the result set to the specified number of records. This means you get the first N records that match your conditions in whatever order the query produces them.

The method returns void and only affects the configuration of the GlideRecord object. It doesn't execute any database operations itself. The limit persists on the GlideRecord object until you create a new GlideRecord or call initialize() to reset it.

Edge cases include passing zero, negative numbers, or non-numeric values as the parameter. ServiceNow silently ignores these invalid values and executes the query without any limit. Fractional numbers are truncated to integers. The limit applies per query execution, so calling query() multiple times on the same GlideRecord will apply the limit each time.

The setLimit() method works alongside other GlideRecord query configuration methods like orderBy(), addQuery(), and chooseWindow(). When combined with chooseWindow(), the limit is applied after the window offset, effectively limiting the windowed results.

When to Use This

Use setLimit() when you need only a specific number of records from a potentially large result set. This is crucial for performance in scenarios like finding "the most recent 5 incidents," "any 10 available assets," or "the first matching record." Without a limit, ServiceNow will retrieve all matching records from the database, consume memory to store them, and transfer them over the network, even if your script only processes a few.

For single record lookups, use get() instead of setLimit(1) followed by query(). The get() method is more efficient and clearer in intent. Avoid setting limits on queries where you genuinely need all results, such as bulk data processing where you'll iterate through every record anyway. Also avoid using setLimit() as a band-aid for poorly designed queries that would otherwise return too much data.

Return Value

The setLimit() method returns void (undefined in JavaScript). It's a configuration method that modifies the GlideRecord object's internal state but doesn't provide any return value. You cannot chain method calls after setLimit() since it doesn't return the GlideRecord object itself.

Since there's no return value, there's no failure state to handle. The method will accept any parameter type, but only positive integers will actually limit the query results. Invalid parameters are silently ignored rather than throwing errors, which means your code will continue executing but without the intended limit applied.

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, ACLs, or other server-side logic execute when calling setLimit() since it only configures the query without executing it
  • Nothing is written to the database or audit tables when setting a limit
  • Performance is excellent - the method executes in microseconds and only modifies object properties in memory
  • The limit setting is not cached and applies only to the current GlideRecord object instance
  • Behaves identically in Business Rules, Script Includes, Scheduled Jobs, and other server-side contexts
  • When the subsequent query executes, the database engine applies the limit at the SQL level, reducing network traffic and memory usage
⚠️

Must be called before query() or it has no effect. Calling setLimit() after query() will only affect subsequent query executions.