Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Array returns "undefined".

Victor Ruiz
Tera Guru

Attempting to retrieve a list of CI's but the second Glide query returns undefined, what am I missing?

 

var appsArr = [];

var gr2 = new GlideRecord('task_ci');
gr2.addQuery('task', current.sys_id);
gr2.query();
while(gr2.next()){

appsArr.push(gr2.ci_item.toString());

}

var applications = [];

for(var i=0; i<appsArr.length; i++){

var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('child',appsArr[i]);
gr.query();
gs.log(gr.parent); //RETURNS UNDEFINED
while(gr.next()) {

applications.push(gr.parent.toString());
}
}
current.addQuery('sys_id','IN',applications.toString());

 

 

20 REPLIES 20

my bad pasted wrong code. 

 

i tested with your script on my instance and i am getting correct output. 

 

make sure you have record in cmdb_rel_ci table. 

 

Do one thing try to hard-cord the on task sysid and see the result. 

 

var appsArr = [];

var gr2 = new GlideRecord('task_ci');
gr2.addQuery('task','8857dba437501000bff8e0f6177334fc');
gr2.query();
while(gr2.next()){

appsArr.push(gr2.ci_item.toString());

}
gs.print(appsArr);

var applications = [];

for(var i=0; i<appsArr.length; i++){

var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('child',appsArr[i]);
gr.query();

while(gr.next()) {

applications.push(gr.parent.toString());
}
}
gs.print(applications);

returning NULL.

 

this is what i tried on my personal instance and it's giving me value.

 

find_real_file.png 

find_real_file.png

 

 

I just tried on my personal instance and it did work.  The culprit here is the gliderecord on cmdb_rel_ci where it is not returning any values, but I do see the values in the array.

 

AbhishekGardade
Giga Sage

Hello Victor,

Try below code and let me know

var appsArr = [];

var gr2 = new GlideRecord('task_ci');
gr2.addQuery('task', current.sys_id);
gr2.query();
while(gr2.next()){

appsArr.push(gr2.ci_item.toString()); //what is ci_item here?

}

gs.log(appsArr.toString()); // check out the values

var applications = [];

for(var i=0; i<appsArr.length; i++){

var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('child',appsArr[i].toString());
gr.query();

while(gr.next()) {

gs.log(gr.parent); 

applications.push(gr.parent.toString());
}
}
current.addQuery('sys_id','IN',applications.toString());


Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade