Skip to content
IT Canvass
Development · Lesson

GlideQuery

Quick answer

A safer, more readable alternative to GlideRecord for scoped development.

Key takeaways

  • Field name typos throw instead of returning nothing
  • selectOne returns an optional, so missing records are explicit
  • aggregate covers most GlideAggregate use in fewer lines
  • GlideRecord is still required for some platform APIs

Why it exists

GlideQuery wraps GlideRecord with a fluent API, real return values and errors that fail fast instead of silently returning nothing. Typos in field names throw rather than producing an empty result set, which removes a whole class of bugs.

Reading records

Select returns a stream you map over, and selectOne returns an optional you unwrap with get.

var open = new GlideQuery('incident')
  .where('active', true)
  .where('priority', '<=', 2)
  .orderByDesc('opened_at')
  .limit(10)
  .select('number', 'short_description')
  .toArray(10);

var one = new GlideQuery('sys_user')
  .where('user_name', 'abel.tuter')
  .selectOne('sys_id', 'email')
  .get();

Writing and aggregating

Insert, update and deleteMultiple read clearly, and aggregate replaces GlideAggregate boilerplate.

  • insert returns the created record fields you asked for
  • update takes a sys_id and a changes object
  • aggregate supports count, sum, avg, min and max with groupBy
  • disableWorkflow and forceUpdate exist when you genuinely need them

Want to learn this properly?

Our live, instructor-led ServiceNow Training covers this hands-on, with real projects and a certification path.

Check your understanding

  1. What happens when you misspell a field in GlideQuery?

    • A. Empty results
    • B. An exception
    • C. A warning in the log
    • D. It is ignored
    Show answer

    B. An exception

    Failing fast is the main advantage over GlideRecord.

  2. Which method returns a single optional record?

    • A. select
    • B. selectOne
    • C. getOne
    • D. first
    Show answer

    B. selectOne

    selectOne returns an optional you unwrap with get.

Frequently asked questions

Should we rewrite existing GlideRecord code?

No. Use GlideQuery for new work where it reads better. Rewriting working code buys nothing.

Does it work in global scope?

Yes, though it was designed with scoped development in mind and that is where it is most valuable.
CallWhatsAppEnquire