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 09:14 AM
use gs.log(gr.parent); inside the while block.
you can not access value before the iteration
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();
while(gr.next()) {
gs.log(gr.parent);
applications.push(gr.parent.toString());
}
}
current.addQuery('sys_id','IN',applications.toString());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 09:26 AM
its returning null even though I can see the values of the array.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 10:26 AM
try now
var appsArr = [];
var gr2 = new GlideRecord('task_ci');
gr2.addQuery('task', current.sys_id);
gr2.query();
while(gr2.next()){
appsArr.push(gr2.ci_item);
}
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());
}
}
current.addQuery('sys_id','IN',applications);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 10:44 AM
Removing toString() only retrieves one value in the array.