The CreatorCon Call for Content is officially open! Get started here.

Get array field of glideRecord object

sacha1
Tera Contributor

Hi, 

I was wondering if there was an optimised way of retrieving all field values of a queried glideRecord.

The naive way is iterating through each glideElement and push the field (sys_id for example) to an array. I feel like there should be an optimisation that doesn't require me to iterate with the next function.

 

Any input ?

Thanks in advance

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

it would be nice if you share some context with example and script

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi, 

 

this would be the kind of script i want to optimize :

var arr = [];

var gr = new GlideRecord("sys_user");

while (gr.next){

 arr.push(gr.department);//or some other field

}

 

I know what field values i want, i feel like there should be an optimized/bulk way of retrieving that. Rn this take 20s approx, way too long for my needs.

 

There should be just a handful of departments, why don't you just extract them from cmn_department table?

If you loop over the user table then you will get many duplicates and may have to resolve that too. 

-Anurag

Because i don't need a full department list, i need every department that is linked to a user. I will run a uniqueArray on the result for what it's worth, but it is quicker to go from user table than to check every related list.

ankitshaw625
Tera Expert

You can use GlideQuery's select method to get one or more fields from the list of queried records. More details here.