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

Allen Andreas
Administrator
Administrator

Hi,

Have you checked: requestObjFields.employee_id to see what it contains?

Can you troubleshoot log a row count and see what you're getting back?

And you're saying it's repeating the one employee id it found and that request over and over for that same employee?

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


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

requestObjFields.employee_id contains one employee ID, and is correctly filled.The generated Requests that are being infinitely generated are the correct employee ID that's being transmitted via Postman. The issue is why the while loop is looping infinitely. Thanks for the quick response!

Hi,

Yes, I understand your question. We're asking exploratory questions to help us understand the problem. If your object coming in...was full of 1,1,1,1,1,1,1,1,1,1...then it's going to keep running "infinitely"...for employee id 1.

I also asked a few other questions above as well...which other people below are starting to also ask (because it's nice to know).

Such as the log statement around the row count, etc.

Please also try to comment out pieces starting from the bottom and work your way up. Then slowly uncomment going down to see what's going on.

You could try commenting out the for loop you have as that would be another point of issue if it's looping over and over.

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


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

I'll try those suggestions, thank you for the help!