The CreatorCon Call for Content is officially open! Get started here.

nested while loop problems

allen_mineau
Mega Guru

I'm not getting the results I'm expecting from my queries.     the following code is attempting to return a business service and it's associated admin portals from a cross reference table I created.       the first pass works as expected, all other passes do not.

var Scontract = new GlideRecord('ast_service'); // validation record for Service Contract

var BusXref = new GlideRecord('u_bus_service_portal_xref');   // Cross reference table for business service to portal(s)

                                                                                              if (existingCAN == true){

                                                                                                                              Scontract.addQuery('u_can', can.sys_id);

                                                                                                                              Scontract.query();

                                                                                                                              while (Scontract.next()) {                                                

                                                                                                                                                              BusXref.addQuery('u_bus_service',Scontract.getValue('u_business_service'));

                                                                                                                                                              BusXref.query();

                                                                                                                                                              while (BusXref.next()) {

                                                                                                                                                                                              var u_portal = BusXref.getDisplayValue('u_portal');

                                                                                                                                                                                              PortalX.push(BusXref.getDisplayValue('u_portal'));

                                                                                                                                                              }

                                                                                                                              }                                                                                                                          

                                                                                                                              var arrayUtil = new ArrayUtil();

                                                                                                                              arrayUtil.unique(PortalX);

                                                                                              }

the results I'm expecting:

        business service 1

                            portal 1

                            portal 2

        business service 2

                            portal 1

                            portal 3

The results I'm getting

        business service 1

                            portal 1

                            portal 2

        business service 2

suggestions ?

1 ACCEPTED SOLUTION

Nia McCash
Mega Sage
Mega Sage

Try:



var Scontract = new GlideRecord('ast_service'); // validation record for Service Contract



if (existingCAN == true){


  Scontract.addQuery('u_can', can.sys_id);


  Scontract.query();


  while (Scontract.next()) {                                              


            var BusXref = new GlideRecord('u_bus_service_portal_xref');   // Cross reference table for business service to portal(s)


            BusXref.addQuery('u_bus_service',Scontract.getValue('u_business_service'));


            BusXref.query();


            while (BusXref.next()) {


                      var u_portal = BusXref.getDisplayValue('u_portal');


                      PortalX.push(BusXref.getDisplayValue('u_portal'));


            }


  }                                                                                                                        


  var arrayUtil = new ArrayUtil();


  arrayUtil.unique(PortalX);


}



Note Line 7 was moved because otherwise, you're just adding more and more query conditions to an existing GlideRecord object and will get no matches.


View solution in original post

3 REPLIES 3

Nia McCash
Mega Sage
Mega Sage

Try:



var Scontract = new GlideRecord('ast_service'); // validation record for Service Contract



if (existingCAN == true){


  Scontract.addQuery('u_can', can.sys_id);


  Scontract.query();


  while (Scontract.next()) {                                              


            var BusXref = new GlideRecord('u_bus_service_portal_xref');   // Cross reference table for business service to portal(s)


            BusXref.addQuery('u_bus_service',Scontract.getValue('u_business_service'));


            BusXref.query();


            while (BusXref.next()) {


                      var u_portal = BusXref.getDisplayValue('u_portal');


                      PortalX.push(BusXref.getDisplayValue('u_portal'));


            }


  }                                                                                                                        


  var arrayUtil = new ArrayUtil();


  arrayUtil.unique(PortalX);


}



Note Line 7 was moved because otherwise, you're just adding more and more query conditions to an existing GlideRecord object and will get no matches.


Thank you Nia, that worked.


Had the same issue, this really helped.   Thanks!!