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 10:56 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 11:13 AM
returning NULL.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 11:15 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 11:21 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2019 09:32 AM
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
Abhishek Gardade