How to store the results of a GlideRecord in array

adityasaraf
Kilo Contributor

Hi,

 

I am trying to execute this script in the background.

 

var order=new GlideRecord('sc_cat_item');

                                order.addQuery('category.title','Computer & Telephony');

                                order.addQuery('active',true);

                                order.query();

var i=0;

                                var item_access=new Array();

 

 

                                while(order.next()){

                             

                                item_access[i++]=order;

                           

                                }

                                for(var count=0;count < item_access.length; count++){

gs.log(item_access[count].name);
}

 

 

In the logs of the array it return the last record of the gliderecord multiple times.

Can you guide on what can be done about this?

I would like the array to store all the records of the GlideRecord and display the array iteratively.

 

Kind regards

Aditya

1 ACCEPTED SOLUTION

ohhgr
Kilo Sage
Kilo Sage

Hi Aditya,



It's mostly because the reference is not treated as a string but rather as a glide element.



You can use the order.getValue("sys_id"); to avoid getting same repeated value.




  1. var order=new GlideRecord('sc_cat_item');
  2. order.addQuery('category.title','Computer & Telephony');
  3. order.addActiveQuery();
  4. order.query();
  5. var item_access = [];
  6. while(order.next()){
  7. item_access.push(order.getValue("sys_id"));
  8. }
  9. for(var count=0;count < item_access.length; count++){
  10. gs.log(item_access[count].name);
  11. }


Hope that helps.



Thanks,


Mandar


View solution in original post

2 REPLIES 2

ohhgr
Kilo Sage
Kilo Sage

Hi Aditya,



It's mostly because the reference is not treated as a string but rather as a glide element.



You can use the order.getValue("sys_id"); to avoid getting same repeated value.




  1. var order=new GlideRecord('sc_cat_item');
  2. order.addQuery('category.title','Computer & Telephony');
  3. order.addActiveQuery();
  4. order.query();
  5. var item_access = [];
  6. while(order.next()){
  7. item_access.push(order.getValue("sys_id"));
  8. }
  9. for(var count=0;count < item_access.length; count++){
  10. gs.log(item_access[count].name);
  11. }


Hope that helps.



Thanks,


Mandar


Hello,

Sorry to re-open this feed long time after, but I have same issue than Adityasaraf.

Your solution doesn't work for me as it save the sys_id as string. I loose the reference to the GlideRecord and are not able to retrieve record properties later. 

Here is what I got with following code :

FabienHenriot_0-1679320869050.png

FabienHenriot_1-1679320891697.png

Do you have any other idea? I wanted to avoid creating new query to table via new GlideRecord record using get method as follow :

FabienHenriot_2-1679320956308.png

 

Thanks for answer! ðŸ˜Š

Best regards,

Fabien