Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

GlideRecord Query within Loop Not Working

sareshnaroji
Kilo Explorer

Hello,

We have an Wrapper Function with GlideRecord Query called within a For loop. The GlideRecord Query executes for the first time. However, for subsequent iterations .next(), .has next() returns false.

Any suggestion on using a GlideRecord Query within a for loop will be highly appreciated.

Regards,
Saresh

1 ACCEPTED SOLUTION

Saresh,



  Use this script



getAffectedClients();


function getAffectedClients(){


  var affected_clients = current.u_affected_clients.getDisplayValue();


  if(affected_clients.length!=0){//affected clients list


  getRelManagers(affected_clients);


}




function getRelManagers(rm_name){


  var gr_rm = new GlideRecord('u_rel_mamager_list');


  gr_rm.addQuery("u_client_name",'IN', rm_name);


  gr_rm.query();           //only first iteration runs successfully


  while (gr_rm.next()) {   //does not iterate second time gr_rm.next returns false


  var rel_manager = gr_rm.getValue('email');


  gs.addInfoMessage(rel_manager); //prints only records of first array object


  gs.eventQueue("affectedclients.notify", current, rm_name, rel_manager);


}


}


}


View solution in original post

10 REPLIES 10

tanumoy
Tera Guru

Could you please post your code here.


OnBefore Business Rule Insert/Update



getAffectedClients();



function getAffectedClients(){


var affected_clients = current.u_affected_clients.getDisplayValue(); //affected clients list


var array_clients = affected_clients.split(",");


if(array_clients.length !=0){



for(var count =0; count < array_clients.length; count++){



getRelManagers(array_clients[i]);


}


}



function getRelManagers(rm_name){


var gr_rm = new GlideRecord('u_rel_mamager_list');


gr_rm.addQuery("u_client_name", rm_name);


gr_rm.query();             //only first iteration runs successfully



while (gr_rm.next()) {     //does not iterate second time gr_rm.next returns false


var rel_manager = gr_rm.email.toString();


gs.addInfoMessage(rel_manager); //prints only records of first array object


gs.eventQueue("affectedclients.notify", current, rm_name, rel_manager);


}


}


Saresh,



  Use this script



getAffectedClients();


function getAffectedClients(){


  var affected_clients = current.u_affected_clients.getDisplayValue();


  if(affected_clients.length!=0){//affected clients list


  getRelManagers(affected_clients);


}




function getRelManagers(rm_name){


  var gr_rm = new GlideRecord('u_rel_mamager_list');


  gr_rm.addQuery("u_client_name",'IN', rm_name);


  gr_rm.query();           //only first iteration runs successfully


  while (gr_rm.next()) {   //does not iterate second time gr_rm.next returns false


  var rel_manager = gr_rm.getValue('email');


  gs.addInfoMessage(rel_manager); //prints only records of first array object


  gs.eventQueue("affectedclients.notify", current, rm_name, rel_manager);


}


}


}


Thank you Abhinay, passing the entire list works fine for looping within the list items