Glide Freaking Query Wow

So I thought this was… just another odd class. It’s so much more.

A month ago I wrote about GlideQuery, asking “… is GlideRecord going away? No. I don’t think so. “

Well, I need to revise my thoughts on this. Before I go into why, lets first link to the sources and you can watch the 20+ minute video of the genius (Peter Bell) behind GlideQuery.

Link to that is here: GlideQuery: A modern upgrade to GlideRecord

Okay. Now that is out of the way lets talk about some of the major points here so if we lose the video we still have this somewhere since as of right now this is not documented anywhere.

What does GlideQuery do that GlideRecord fails?

GlideQuery is a server-side only api for querying, updating, and deleting data. It’s being used in Orlando, and Paris. Behind the scenes it uses GlideRecord but with smart defaults that often will cause problems eventually.

This api follows three guiding principals; - Fail Fast - Be JavaScript - Expressive

There are some considerations; - Performance

Failing Fast

Improving the feedback loop

Field Checking

Here’s a GlideRecord script with a problem By default if a field name is wrong in GlideRecord, it queries everything.

In GlideQuery, this will fail and not execute on error.

GlideRecord ExampleGlideQuery Example

Choice checking

Here’s another example where unless you check your code you may miss it. This is because it just returns no results.

Really if the api said, hey that value is not valid, that could help you faster.

GlideRecord ExampleGlideQuery Example

Type checking

Travis Toulson wrote a whole post about Is GlideRecord GetValue the king of the string… It’s great but this SOLVES that whole issue.

GlideRecord ExampleGlideQuery Example

Be JavaScript

Isolation from Java

GlideQuery should behave you expect a regular JavaScript should behave.

One problem with GlideRecord, things are not always as they appear. Sometimes you’ll get a value to print but where you might think the value is a type of number it’s in fact a string.

GlideRecord ExampleGlideQuery Example

Stacktraces

GlideRecord ExampleGlideQuery Example

Complex Queries should work how you expect

Queries that use both “AND” and “OR” logic. How do you think this is evaluated?

GlideRecord ExampleGlideQuery Example

In SQL “AND” has priority over “OR”. In GlideRecord, “OR” has priority over “AND” and this causes problems. In GlideQuery they solve this by being allowing nested GlideQuery methods.

Expressive

Do more with less

Returning a Stream or Optional

When reading data with GlideQuery there are two classes used; Stream and Optional

StreamOptional
Used for reading multiple recordsUsed for reading a single record
Returned by select(1)Is considered “empty” if a record isn’t found by a query
Is lazily evaluatedReturned by selectOne(), insert(), and update()
Common Stream methods:
- map
- flatMap
- forEach
- reduce
- some
- any
Common Optional methods:
- get [throws if empty]
- map
- isEmpty
- isPresent
- ifPresent
- orElse

Examples;

Aggregation

Examples;

Insert

Delete

Update

Field Flags

This is important for currency and display values.

Flags mentioned $DISPLAY and $CURRENCY_CODE

Performance

With business rules disabled here’s how GlideQuery stacks up to GlideRecord.

MethodGlideRecordGlideQuery
Insert 100 records829 ms median850 ms median (+2.5%)
Reading 1 record2ms median3ms median (+1 ms)
Reading 1,000 record86.5 ms median90.5 ms median (+4.62%)
Reading 10,000 record842.5 ms median890 ms median (+5.64%)

It’s important to note that GlideQuery avoids common performance mistakes.

Current Limitations and Future work

  • Scoped table permission checking
  • Allow opting out of field/choice checking
  • Better join support
  • Field casting
  • Parsing encoded queries

Edit this page

Avatar
Jace Benson
ServiceNow Developer

ServiceNow is my day job, JAMStack is my passion.