- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2022 08:49 AM
Hello- I am new learner of Javascript and trying to extract records of same table. My code is as below:
var target = new GlideRecord('cmdb_rel_ci');
var i=0, j=0;
var OYarray=[];
var newParent;
target.addEncodedQuery('parent.u_service_team=aps^parent.u_ci_id=CI000000001^type=41008aa6ef32010098d5925495c0fb94^child.sys_class_name=cmdb_ci_service');
target.query();
gs.print('Total Records: '+ target.getRowCount());
while(target.next())
{
OYarray.push(target.child.name.toString());
for(i=0;i<=OYarray.length;i++) {
newParent=OYarray;
target.addEncodedQuery('parent.name=' +newParent+'^child.sys_class_name!=cmdb_ci_service');
target.query();
gs.print('New Child: '+target.child.name);
}
}
I am trying to 1- extract child of parent, 2- then pass child as parent 3- extract child of new parent. I am failing nested conditions at 2nd 3rd steps. Please suggest.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2022 04:00 AM
Hi GM,
Something like below. There's a need to create a new GlideRecord to search and there's a need to do a .next() to get each item from the result.
var target = new GlideRecord('cmdb_rel_ci');
var i = 0,
j = 0;
var OYarray = [];
var newParent;
target.addEncodedQuery('parent.u_service_team=aps^parent.u_ci_id=CI000000001^type=41008aa6ef32010098d5925495c0fb94^child.sys_class_name=cmdb_ci_service');
target.query();
gs.print('Total Records: ' + target.getRowCount());
while (target.next()) {
OYarray.push(target.child.name.toString());
for (i = 0; i <= OYarray.length; i++) {
newParent = OYarray[i];
var target2 = new GlideRecord('cmdb_rel_ci');
target2.addEncodedQuery('parent.name=' + newParent + '^child.sys_class_name!=cmdb_ci_service');
target2.query();
while (target2.next()) {
gs.print('New Child: ' + target2.child.name);
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2022 09:12 AM
Hey,
In first line:
newParent=OYarray[i];// you are just passing the whole array, instead you should pass the array element
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2022 01:47 AM
Done that but result is same incorrect.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2022 04:00 AM
Hi GM,
Something like below. There's a need to create a new GlideRecord to search and there's a need to do a .next() to get each item from the result.
var target = new GlideRecord('cmdb_rel_ci');
var i = 0,
j = 0;
var OYarray = [];
var newParent;
target.addEncodedQuery('parent.u_service_team=aps^parent.u_ci_id=CI000000001^type=41008aa6ef32010098d5925495c0fb94^child.sys_class_name=cmdb_ci_service');
target.query();
gs.print('Total Records: ' + target.getRowCount());
while (target.next()) {
OYarray.push(target.child.name.toString());
for (i = 0; i <= OYarray.length; i++) {
newParent = OYarray[i];
var target2 = new GlideRecord('cmdb_rel_ci');
target2.addEncodedQuery('parent.name=' + newParent + '^child.sys_class_name!=cmdb_ci_service');
target2.query();
while (target2.next()) {
gs.print('New Child: ' + target2.child.name);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2022 05:01 AM
Thanks much