Nested loop conditions

GM4
Mega Contributor

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.

 

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

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);
        }
    }
}

View solution in original post

4 REPLIES 4

Aman Kumar S
Kilo Patron

Hey,

In first line:

newParent=OYarray[i];// you are just passing the whole array, instead you should pass the array element

Best Regards
Aman Kumar

GM4
Mega Contributor

Done that but result is same incorrect.

Hitoshi Ozawa
Giga Sage
Giga Sage

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);
        }
    }
}

Thanks much @Hitoshi Ozawa . It worked, however I still see some records have value as 'Undefined' in second while. Validating the data.