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
Giga 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();
}
}

 

View solution in original post

8 REPLIES 8

Jason59
Mega Contributor

Aman I actually tried your way first but couldn't get it to work.

Ankur Bawiskar
Tera Patron

Hi,

you can use below query for array

var array = ['sys_id1','sys_id2'];

grScheduledImport.addQuery('sys_id', 'IN', array);

Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

ARG645
Tera Guru

Efficient and Optimized

 Pro Tip: Use updateMultiple() for Maximum Efficiency!

var arr = ['sys_id1','sys_id2','sys_id3'];//pass your sys_id's comma separated


var gr = new GlideRecord('sys_data_source');
gr.addQuery('sys_id','IN',arr);
gr.setValue('last_run_datetime','');
gr.updateMultiple();

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.

Aman Gurram

Adrian Ubeda
Mega Sage

Hi, 

I let you an example about incident query and how to create an array made from GrideRecord sys_id's or GlideRecord fields:

var array = [];

var gr = new GlideRecord('incident');
gr.addQuery('caller_id',sys_id_value);
gr.query();

while(gr.next()) {
  array.push(gr.sys_id);
}

//If you want to use for another query you can use it as follow
var gr1 new GlideRecord('incident');
gr1.addEncodedQuery('sys_idIN' + array.join());
gr1.query();

In this case, you only need to iterate over GlideRecord and store with push all the attributes that you need.

If it was helpful, please give positive feedback.

Thanks,