How to iterate array using for loop and pass each value in a gliderecord

Laukik Udpikar
Tera Expert

Hello Experts,

I need help in below script;

arr = ['sysid,'sysid'];

I want to create a for loop and get each sysid to be passed in a gliderecord and add each value as part of encodeded query as well,

Need help in building it

1 ACCEPTED SOLUTION

Hi,

by iterating every sysId you want to fetch the child

do like this

var arr = ['sysid1','sysid2'];

var child = [];

for(var i in arr){
	var gr = new GlideRecord('table');
	gr.addEncodedQuery('parent!=NULL^active=true^parent=' + arr[i]);
	gr.query();
	while(gr.next()){
		child.push(gr.sys_id.toString());
	}
}

Regards
Ankur

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

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

so what have you started with and where are you stuck?

Regards
Ankur

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

I'm going to guess they're stuck at "missing ] after element list" since their array isn't valid 😉

var arr = ['sysid','sysid'];

var child = [];

for(var j =0; j < arr.length ; j++){

//var eq = 'parent!=NULL^active=true^parent='+arr[j];

var gr = new GlideRecord('table');

//gr.addEncodedQuery(eq);

gr.addQuery('parent',arr[j]);

gr.query();

while(gr.next()){

child.push(gr.sys_id);

}

}

@Laukik Udpikar 

try this

var arr = ['sysid1','sysid2'];

var child = [];

var gr = new GlideRecord('table');
gr.addEncodedQuery('parent!=NULL^active=true^parentIN' + arr.toString());
gr.query();
while(gr.next()){
	child.push(gr.sys_id.toString());
}

Regards
Ankur

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