What It Does

The orderByDesc() method configures a GlideRecord query to sort results in descending order by the specified field. When the query executes, ServiceNow's database layer adds an ORDER BY clause with DESC to the generated SQL statement. The sort order applies to all records returned by the query, whether you iterate through them or just retrieve the first result.

Internally, ServiceNow builds the ORDER BY clause before sending the query to the database. The method accepts any valid field name on the target table, including system fields like sys_created_on or number, and custom fields. You can also dot-walk to fields on referenced tables, though this impacts query performance as it requires table joins.

The method returns void and only configures the query—it doesn't execute it. The sorting takes effect when you call query(), queryNoDomain(), or get(). If you call orderByDesc() after query execution, it has no effect on the already-retrieved results.

When sorting by date/time fields, descending order shows the most recent records first—exactly what you want for activity streams, recent changes, or "latest first" displays. For numeric fields like incident numbers, descending order shows the highest numbers first. String fields sort in reverse alphabetical order, with 'Z' values appearing before 'A' values.

Multiple orderByDesc() and orderBy() calls can be chained together for multi-level sorting. The first call sets the primary sort order, the second call sets the secondary sort order, and so on. This relationship with orderBy() makes them complementary methods for building complex sort logic.

When to Use This

Use orderByDesc() when you need the most recent, highest, or "latest first" records. The most common use case is sorting by sys_created_on or sys_updated_on to display recently created or modified records first. This is essential for building activity feeds, recent changes reports, or any UI where users expect to see the latest activity at the top.

Choose orderBy() instead when you want ascending order—oldest first, lowest numbers first, or alphabetical order. Avoid the common mistake of using orderByDesc() for every sort operation. If you're building reports where users expect chronological order (oldest events first), or alphabetical lists, use orderBy() instead.

Don't use ordering methods when you only need to check if records exist or count them. Methods like hasNext() or getRowCount() don't benefit from sorting and the database has to do extra work to sort results you won't use.

Return Value

The method returns void (undefined in JavaScript). It's a configuration method that modifies the GlideRecord object's internal query state rather than returning data. You cannot chain orderByDesc() calls together like gr.orderByDesc('field1').orderByDesc('field2') because the method doesn't return the GlideRecord object.

The method never fails or throws exceptions, even if you pass an invalid field name. ServiceNow will attempt to sort by whatever string you provide, and if the field doesn't exist, the database typically ignores the invalid ORDER BY clause. This silent failure can make debugging difficult if you misspell field names.

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 notifications fire when calling this method—it only configures query behavior
  • Nothing gets written to the database; the method only affects how data is retrieved
  • Performance impact varies by field type—sorting by indexed fields is fast, unindexed fields require full table scans
  • Results are not cached—each query execution sorts the current data state from the database
  • Works identically in Business Rules, Script Includes, and Scheduled Jobs—no behavioral differences between execution contexts
  • Sorting by dot-walked fields creates SQL joins which significantly impact performance on large tables
  • The sort order persists for the life of the GlideRecord object but doesn't affect other GlideRecord instances