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

Hmm...I ran just this in background script just to see

var applications = [];

for(var i=0; i<7; i++){

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

while(gr.next()) {
applications.push(gr.getValue("parent")+"");
}
}
gs.print('final list of apps'+applications.toString());

And I'm getting results...so then there may be an issue with your initial array....


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen....did you get any values from "gr.addQuery('child',appsArr[i]+""); ??

That's where it I'm returning null.

 

I just tried this and got results:

var appsArr = [];
var gr2 = new GlideRecord('task_ci');
gr2.addQuery('task', current.sys_id);
gr2.query();
while(gr2.next()){
gs.log("Test" + gr2.ci_item);
appsArr.push(gr2.ci_item.sys_id);
}

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.sys_id);
}
}
gs.log('final list of apps'+applications.toString());

The only difference is I just a hard-coded task sys_id...so as long as you're using current correctly...this should work.

 

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

I can see the values in the array but the apps are still null. 

 

Thanks Allen,, it worked for me. After adding sys_id , it did wonder 🙂