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

Try something like below:



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[count]);


}


}



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


}


}


This is incorrect. Why are you using current.rm_name?? rm_name is just a parameter passed to the function.


Sorry for the mistake. corrected it.


Np. Just letting you know


Thanks Abhinay, but affected_clients is a list and i want to process each of the list items with the GlideRecord.


Let me try this and see if it works




Tanumoy,


getRelManagers(array_clients[count]); "i" was a typo from my side



Thanks