- 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 01:00 PM
So I did a log and it returned as 669 for the gr.getRowCount. I don't understand what it is that's causing it.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2020 03:10 PM
The problem is, with gliderecord, is if ANY of your addQuery parameters, addEncodedQuery etc. don't make "sense", SN doesn't error out the query, it just ignores that line:
var gr = new GlideRecord('sys_user');
gr.addQuery('employee_number',requestObjFields.employee_id);
gr.addQuery('active',true);
gr.query();
So it's safe to say that your your active query line is fine, but the employee number is not....
You can double check this by simply commenting out the employee number part of your query and I bet you'll see the same row number print: 669?
Please address the employee number add query line and ensure that the object coming in is string for that to be interpreted by the gliderecord.
Print requestObjFields.employee_id.toString()?
Ensure it's string.
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-04-2020 08:43 AM
So I commented out the line "//gr.addQuery('employee_number',requestObjFields.employee_id);" and now it's returning 3878. So the line is filtering, just not correctly? Now I'm really confused...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2020 09:22 AM
If you pass in a variable that is undefined or null then ServiceNow runs your query as employee_number=NULL and active=true. If you comment out the line, then it runs with no filter for employee_number at all and just active=true.
What you need to do is check for a null or undefined variable value and exit out of your script. Here's one way. This does an implicit type cast to Boolean, so you need to understand what happens to employee_id if you cast it to Boolean (e.g. numeric 0, empty string, null, undefined all become false).
if (!requestObjFields.employee_id) return;
Put this up at the first line of your code before you get into the loop. There are lots of forms of checking for null/undefined. Make sure to use the form that matches your business requirements. For example, some cases might require doing one thing with numeric 0 and a different thing for empty string, undefined or null.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2020 09:45 AM
Hi,
Yea, so it was filtering to some extent, then.
See Mwatkins response for some explanation from SN for that, but in the end, it appears it's an issue with that object. Please try to double check the contents of that object and perhaps pass as string as I mentioned above.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!