- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2020 06:51 AM
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!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2020 11:54 AM
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2020 07:00 AM
Hi,
From where is this coming?
requestObjFields.employee_id
What is the type from which you fetch this?
Thanks,
Ashutosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2020 07:04 AM
It's coming from earlier in the API. requestObjFields is what receives the information in the API:
var requestObjFields = {
"employee_id":"",
"pt_ft_status":"",
"employee_type":"",
"last_hire_date":"",
"termination_date_time":"",
"supervisor_employee_id":"",
"two_week_notice":"",
"employee_id_to_forward_email":"",
"employee_id_to_forward_vociemail":""
It's properly receiving the Employee ID from this field, the issue is that the while loop keeps creating the Request for the 1 returned Emp ID over and over.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2020 07:44 AM
HI,
Are you sure that employee number is unique?
Thanks,
Ashutosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2020 07:58 AM
I'm not sure what you mean. I'm inputting 1 employee ID into Postman for the testing I'm doing. The output of the API is that employee ID, so it is generating for that one employee ID, it's just generating repeatedly over and over. This is why I stated I believe it's the while loop that's the issue somehow.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2020 08:24 AM
Hi,
What i mean is in your system you have that employee id for one user only right.
For testing remove the while loop and use if
Thanks,
Ashutosh