Array returns "undefined".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 09:08 AM
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());
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 11:23 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 11:27 AM
Hi Allen....did you get any values from "gr.addQuery('child',appsArr[i]+""); ??
That's where it I'm returning null.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 11:29 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 11:32 AM
I can see the values in the array but the apps are still null.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2020 04:40 AM
Thanks Allen,, it worked for me. After adding sys_id , it did wonder 🙂