How to restrict columns retrieved in Glide Query

rohanhd
Kilo Contributor

Hi All, we are all familiar with query using Glide Record. But when we query table for e.g. sys_user we can restrict the number of records retrieved by specifying addQuery conditions.

But this retrieves all the columns within the record. It impacts performance if there are many glide calls. Question is : is there a way to restrict number of columns retrieved in Glide Record.

Is there something I can do to say I only want a certain set of columns.

9 REPLIES 9

Chuck Tomasi
Tera Patron

The only way to do this presently, Rohan, is via the REST API using the sysparm_fields argument.



REST API - ServiceNow Wiki



See the REST API explorer to create an example.



REST API Explorer - ServiceNow Wiki



Via script, there is currently no supported way. I love the idea. I invite you to open an enhancement request. Our product managers are listening!


Enhancement requests: Tell us how you would improve the ServiceNow product


Tony DiRienzo
Giga Guru

I know it has been a while since this question was posted, but I thought I would share an answer I found.

You can limit your query to specific fields in a server-side Glide query by using GlideAggregate instead of GlideRecord to query the table.  For example, if you are querying sys_user, but only want back the user_name field:

var ga = new GlideAggregate('sys_user');
ga.groupBy('user_name');
ga.groupBy('sys_id'); // This makes sure we get all records, even if there are duplicates
ga.query();

while (ga.next()) {
  // Do what you need to with the user_name field
}

Just add another groupBy() for each field you need included in the results.  I've seen these kind of GlideAggregate queries run up to 5 times faster than a GlideRecord query for the same data set.

Be aware that you will lose most (if not all) of the performance gain if you dot-walk in the results.

This is fantastic!

Very sneaky. I like it!

I was running some tests, and I can attest to the near 5 times faster speed. This is awesome!

I know ServiceNow more recently release "GlideQuery" which has support also for pulling a limited number of columns. I wonder how these two compare in terms of speed.