What about GlideQuery ?

Bruno Leclerc
Kilo Expert

Hi,

Does anyone use GlideQuery instead of GlideRecord?
This object seems very attractive to me, is very well documented on docs.snow.
Its source code is readable. But I can't find much about it on the community... What do you think ?

 

9 REPLIES 9

@Hitoshi Ozawa regarding your example above, I would refactor your approach to this problem. Rather than a nested query (regardless of GlideRecord or GlideQuery), I would execute the first query to gather all of the users' locations by display name and call it a day... GlideQuery would make this request much easier and MUCH faster. 

The query below:

new GlideQuery('sys_user')
.where('manager.user_name', 'jess.assad')
.select('user_name', 'manager.name', 'manager.location.name')
.toArray(10);

 

will return the results below:

[
  {
    "user_name": "krystle.stika",
    "manager": {
      "name": "Jess Assad",
      "location": {
        "name": "2-10-1 Yurakucho, Chiyoda-ku, Tokyo"
      }
    },
    "sys_id": "02826bf03710200044e0bfc8bcbe5d7f"
  },
  {
    "user_name": "naomi.greenly",
    "manager": {
      "name": "Jess Assad",
      "location": {
        "name": "2-10-1 Yurakucho, Chiyoda-ku, Tokyo"
      }
    },
    "sys_id": "0382abf03710200044e0bfc8bcbe5d42"
  }
]

Your first example will not work because you select 'user_name' but then attempt to use 'location'. Plus it is very inefficient as you could have just dot-walked to the location name without the extra query. Plus not much point assigning it to a variable.

 

new GlideQuery("sys_user")
  .where("manager.user_name", "abel.tuter")
  .select("location.name")
  .forEach(function (user) {
    gs.info(user.location.name);
  });

 

Second example only needs to be like this

 

var gr = new GlideRecord("sys_user");
gr.addQuery("manager.user_name", "abel.tuter");
gr.query();
while (gr.next()) {
  gs.info(gr.location.name);
}

 

 

@Hitoshi Ozawa good day.

ServiceNow internal developers are using GlideQuery in the platform development, so I would say it's stable and production-ready.

Adding encoded query support is in the works... I thought it was added in Rome, but cannot find any docs to support that.

GlideQuery is a wrapper for GlideRecord AND GlideAggregate so there is some translation and added overhead.
- Speed is not the reason for GlideQuery, but it should also not be a reason to avoid it. The difference should be small if you define your query effectively.
- Memory usage should be similar as it is only a wrapper for what you are already using.
- GlideQuery is a safer option that will raise errors rather than blindly attempting a database action with a bad filter... potentially corrupting data.
- GlideQuery results are reusable so you do not need to re-query the database... making this a faster option in circumstances where it applies.
- GlideQuery produces results that are native JSON data, making them easier to use and easier for developers new to ServiceNow to learn.

The whole no/low-code push has its place, but there are some skills that come with learning to code that are missed. The ability to articulate a process into clear, discreet requirements is derived from IT and people who think they can jump into ServiceNow without that skill are going to struggle. I suspect this means the developers will spend more time cleaning up and coaching non-developers over time. I could be wrong and would be interested in feedback from teams that have had citizen non-developers for some time.

 

Good luck!

John Dahl
Tera Guru

@Bruno Leclerc I use GlideQuery where it makes sense and as I said in my reply to @Hitoshi Ozawa, ServiceNow internally uses it. The more I use it, the more I like it. It's a safer option and covers most of the situations I find myself using. I also find it more expressive so I can do more with fewer queries. In my response below, I stated in effect that transaction-for-transaction, GlideQuery is going to be a little slower; however, I can refactor my queries to make fewer transactions... often improving performance.

Where I would exercise caution is inside your organization. Will the rest of your team be able to support the solutions you create with GlideQuery? Do you have the option of providing lunch-and-learn style sessions to explain GlideQuery and get feedback about the team's willingness to learn and adopt it?

Bruno Leclerc
Kilo Expert

Hi All,

Thank's a lot for your interesting answers.

what I understand is that it is promising but be careful all the same.

I think I will invest some time on this object with all the recommendations you mention.

This code is still young it will probably evolve, especially if OOTB starts using it.

Once again thank you.