While loop stuck infinitely repeating

Christopher Ros
Giga Contributor

Hello everyone! So I have an API that creates Requests. I've recently been coding to account for duplicate returns. The expected behavior is a while loop that is supposed to cycle through every active employee ID it finds, creating a REQ for each, then finish. For whatever reason it's repeating the one return infinitely. 

Here's the code:

var gr = new GlideRecord('sys_user');
gr.addQuery('employee_number',requestObjFields.employee_id);
gr.addQuery('active',true);
gr.query();
while(gr.next()){
var EmployeeTermCatalogItemID = "5cd3e62bdbdb7300ceec3caf9d96196d";
var catalogItemVariables = buildCatalogItemVariables(RequestObject);
if(error.occured){
return error.content;
}
var cart = new Cart(null,catalogItemVariables.term_supe);
var item = cart.addItem(EmployeeTermCatalogItemID,1);

for(var key in catalogItemVariables){
var value = catalogItemVariables[key];
cart.setVariable(item,key,value);
}
var reqGR = cart.placeOrder();
response.setBody({
'Number':reqGR.getValue('number'),
'URL':"https://"+gs.getProperty('instance_name')+".service-now.com/sp?id=sc_request&sys_id="+reqGR.getUniqueValue()+"&table="+reqGR.getTableName()
});
}

 

Any help is GREATLY appreciated! Thank you!

1 ACCEPTED SOLUTION

It's absolutely no problem. Keep asking questions!

So per your API code you posted above...it's setting it to null?

 "employee_id":"",

So...while you're using the key and value, per that script, it's setting it to "null" aka nothing.

And then in your query, as Mwatkins mentioned, it's basically treating it as if you're asking for:

gr.addQuery('employee_number', '');

Null.

Try that as a test search and I would imagine you'll see that you get 669 records back or whatever that count was as if when you used

gr.addQuery('employee_number',requestObjFields.employee_id);

Let us know?

So you need to check what you're getting for requestObjFields.employee_id literally and you'll see that it must be a payload issue. So your object is being built with no data, basically...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

31 REPLIES 31

The employee ID is only for one user, but there may be multiple matches due to a name change causing a 'duplicate' That's the whole reason for the first lines is to build a query to pull all the returns for that one Employee ID, then for each employee ID found that's active, with that Employee ID, create a Request. Not sure if I'm explaining that correctly, sorry.

MrMuhammad
Giga Sage

Hi,

When there is some issue with GlideRecord query OOB it returns all the records. Can you try putting a log to display total records return by GlideRecord? if we get only one record then there is some issue inside the while loop and we can take a look into that.  

gs.log("@@ - " + gr.getRowCount());

While(gr.next()){
  .... 

}

 

Please mark this accepted/helpful, if applicable.

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad

Returned 669 results. I don't even know what to do to correct this =(

It returned 669 when I did the getRowCount, so that must be the issue?

Yes, that must be the issue. Can you please try using 

.
.
.
gr.setLimit(3); //return only 3 records
gr.query();

 

and inside while loop can you add log like

while(gr.next()){

   gs.log("@@ - " + gr.user_id + "----" + requestObjFields.employee_id + "----" + gr.employee_number );
   .
   .
   .
   .
   .
}

it will give you User Id, Employee Id and the Employee number you are passing as an input. If the Employee Number and Employee ID is matching then you should validate the data in the sys_user table by filtering results based off user id you received in the each log.

Let me know what you received in the logs so that I can further help you. 

Please mark this accepted/helpful, if applicable.

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad