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

Community Alums
Not applicable

Hi @Sacha ,

This is what Chuck has to say:

gr.getValue('sys_id');

gr is a pointer. If you pop the sys_ids in to an array, you get a list of all of the same values (of the last record.)   getValue() copies the value rather than using the reference.

getValue() has saved my life more times than I care to admit. While it's not perfect, it's often the better option to ensure you have the value (especially in loops.)

Also, refer to this : Community Code Snippets - GlideRecord to Object Array Conversion

 

Mark my answer correct & Helpful, if Applicable.

Thanks,
Sandeep

GlideQuery seemed interesting but the toArray is limited to 100 results unfortunately. And i checked the body function, it just iterate through a stream object, so in the end it's the exact same performance as using a regular GlideRecord (a bit less even because i need to map the stream to extract the field i need) .

 

GlideQuery is very interesting nonetheless, thanks for that. I will be implementing it, feels way more readable than glideRecord.

 

 

As for your other link, it is using the regular method i described in the initial question unfortunately

suvro
Mega Sage

What exactly do you want? Can you elaborate more ? I am afraid thats the only and optimized way of retrieving record 

 

You can refer GlideQuery() if it suits you.

https://developer.servicenow.com/blog.do?p=/post/glidequery-p1/

sacha1
Tera Contributor

Yeah, GlideQuery is interesting but isn't more optimized unfortunately.

 

I don't think there is a better way to do it either. I thought that SN would provide some kind of tools for that need, since it seems like something i would want to often do in the same way we sometimes need to bulk update or delete, and iterating through GlideElement seems to have poor performances when comparing with database operation of the same order.