Development · LessonBy Sneha I, ServiceNow Trainer, 8 yrs · Published · ServiceNow · all levels
GlideQuery
A safer, more readable alternative to GlideRecord for scoped development.
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
Practice challenge
+0 XPStreak ×0
Question 1 of 2
What happens when you misspell a field in GlideQuery?
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.