How to create an array with gliderecord

Jason59
Mega Contributor

Hi I am new to servicenow and wanted to create an array of sys_id with gliderecord

This is my current code using a single sys_id but what if I needed to do say 10 sys_id. How would i put that into an array? What is the best way to do this?

find_real_file.png

 

1 ACCEPTED SOLUTION

Prateek kumar
Mega Sage

Try below:

var arr = [];//pass your sys_id's comma separated

for(var i=0; i<arr.length; i++){
var gr = new GlideRecord('sys_data_source');
gr.addQuery('sys_id',arr[i]);
gr.query();
if(gr.next()){
gr.last_run_datetime = '';
gr.update();
}
}

 


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

View solution in original post

8 REPLIES 8

Kajal Goti
Mega Guru

Hi jason,

 

You can use simply array use in java script:

var arrayDemo=[];

 

Example

  • var order=new GlideRecord('sc_cat_item');
  • order.addQuery('category.title','Computer & Telephony');
  • order.addActiveQuery();
  • order.query();
  • var item_access = [];
  • while(order.next()){
  • item_access.push(order.getValue("sys_id"));
  • }
  • for(var count=0;count < item_access.length; count++){
  • gs.log(item_access[count].name);
  • }

Please mark as correct and helpful,If you find any help worthy

 

Prateek kumar
Mega Sage

Try below:

var arr = [];//pass your sys_id's comma separated

for(var i=0; i<arr.length; i++){
var gr = new GlideRecord('sys_data_source');
gr.addQuery('sys_id',arr[i]);
gr.query();
if(gr.next()){
gr.last_run_datetime = '';
gr.update();
}
}

 


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Thanks Prateek!

 

I had this same javascript written except i had 'var gr = new GlideRecord('sys_data_source'); on the outside of the for loop the whole time so it wouldn't run correctly. It was as simple as just adding it in the for loop smh.

That would work. But that's a very inefficient way.