---
original_url: "https://jace.pro/blog/glide-freaking-query-wow/"
format: markdown
ai_optimized: true
---

Glide Freaking Query Wow- # Glide Freaking Query Wow

May 24, 2020 [servicenow](/tags/servicenow/)

  Enable AI AnimationA month ago I [wrote about GlideQuery](https://jace.pro/post/2020-04-28-what-is-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](https://events.servicenow.com/widget/servicenow/knowledge2020/myagenda/session/1581555110988001mNP1?sessionId=1581555110988001mNP1)

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](https://docs.servicenow.com/search?q=GlideQuery) [documented](https://developer.servicenow.com/dev.do#!/search/orlando/All/GlideQuery) **anywhere**.

# [](#what-does-glidequery-do-that-gliderecord-fails)[What does GlideQuery do that GlideRecord fails?](#what-does-glidequery-do-that-gliderecord-fails)

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

This api follows three guiding principals;

Fail Fast
- Be JavaScript
- Expressive

Here’s some considerations;

- Performance

## [](#failing-fast)[Failing Fast](#failing-fast)

Improving the feedback loop

### [](#field-checking)[Field Checking](#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[![](/img/QNZFBNbOkT-650.jpeg)](/static/img/fieldcheckinggr.png)[](/static/img/fieldcheckinggq.png)### [](#choice-checking)[Choice checking](#choice-checking)

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

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

GlideRecord ExampleGlideQuery Example[![](/img/ewgf_iFA-m-650.jpeg)](/static/img/choicecheckinggr.png)[](/static/img/choicecheckinggq.png)### [](#type-checking)[Type checking](#type-checking)

Travis Toulson wrote a whole post about [Is GlideRecord GetValue the king of the string](https://codecreative.io/blog/is-gliderecord-getvalue-the-king-of-the-string/)… It’s great but this **SOLVES** that whole issue.

GlideRecord ExampleGlideQuery Example[![](/img/FrY5EKZwZT-650.jpeg)](/static/img/typecheckinggr.png)[](/static/img/typecheckinggq.png)[](/static/img/typecheckinggrupdate.png)[](/static/img/typecheckinggqupdate.png)## [](#be-javascript)[Be JavaScript](#be-javascript)

Isolation from Java

### [](#glidequery-should-behave-you-expect-a-regular-javascript-should-behave)[GlideQuery should behave you expect a regular JavaScript should behave.](#glidequery-should-behave-you-expect-a-regular-javascript-should-behave)

Type assumptions in are a real headache in ServiceNow with GlideRecord. It seems you get back a string type when you should get a number type.

GlideRecord ExampleGlideQuery Example[![](/img/IjCeUwyt1g-650.jpeg)](/static/img/bejavascriptstringlytyped.png)[](/static/img/bejavascriptstringlytypedgq.png)[](/static/img/bejavascriptstringlytyped2.png)[](/static/img/bejavascriptstringlytyped3.png)### [](#stacktraces)[Stacktraces](#stacktraces)

GlideRecord ExampleGlideQuery Example[](/static/img/stacktracegr.png)[](/static/img/stacktracegq.png)### [](#complex-queries-should-work-how-you-expect)[Complex Queries should work how you expect](#complex-queries-should-work-how-you-expect)

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

GlideRecord ExampleGlideQuery Example[![](/img/NMO6rBl0mZ-650.jpeg)](/static/img/complexqueriesgr.png)[](/static/img/complexqueriesgq.png)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)[Expressive](#expressive)

Do more with less

### [](#returning-a-stream-or-optional)[Returning a Stream or Optional](#returning-a-stream-or-optional)

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

StreamOptionalUsed for reading recordsUsed for reading a single recordReturned by `select(1)`“Empty” if a record isn’t found by a queryLazily evaluatedReturned by `selectOne()`, `insert()`, and `update()`Common Stream methods: - map - flatMap - forEach - reduce - some - anyCommon Optional methods: - get [throws if empty] - map - isEmpty - isPresent - ifPresent - orElseExamples;

[](/static/img/maponstream.png)[](/static/img/someonstream.png)[](/static/img/everyonstream.png)

### [](#aggregation)[Aggregation](#aggregation)

Examples;

[](/static/img/aggregate1.png)[](/static/img/aggregate2.png)[](/static/img/aggregate3.png)

### [](#insert)[Insert](#insert)

[](/static/img/insert.png)

### [](#delete)[Delete](#delete)

[](/static/img/delete.png)

### [](#update)[Update](#update)

[](/static/img/updateone.png)[](/static/img/updatemultiple.png)

### [](#field-flags)[Field Flags](#field-flags)

This is important for currency and display values.

Flags mentioned `$DISPLAY` and `$CURRENCY_CODE`

[](/static/img/flagscurrency.png)

## [](#performance)[Performance](#performance)

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

MethodGlideRecordGlideQueryInsert 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.

[](/static/img/commonperfmistakes.png)

[](/static/img/commonperfmistakes2.png)

## [](#current-limitations-and-future-work)[Current Limitations and Future work](#current-limitations-and-future-work)

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

---
[View this page on GitHub](https://github.com/jacebenson/jace.pro/tree/main/./src/posts/2020/2020-05-24-glide-freaking-query-wow.md).

[Glide Freaking Query Wow](https://jace.pro/blog/glide-freaking-query-wow/) [Jace Benson](https://jace.pro) ![Jace Benson](https://jace.pro/icon-512x512.png)

---

*This content is from Jace Benson's ServiceNow and tech blog at jace.pro*
*Original post: https://jace.pro/blog/glide-freaking-query-wow/*
