- 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-04-2020 11:29 AM
I'm so sorry for taking up so much of your time with this and thank you so much for working with me. Really great community.
So this is what it is further up 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":""
this only receives one input for each field from the 'other end' of the API, which is why I'm not getting what could be messed up in the query making it show so many returned results. Is it a syntax thing and I'm not writing the line out properly?
var grUD = new GlideRecord('sys_user');
grUD.addQuery('employee_number',requestObjFields.employee_id);
grUD.addQuery('active',true);
grUD.query();
Besides this I'm at the end of my rope. I cannot figure out where else in the API it could get the Employee ID, as that's the name of the variable it's being provided from the other end in the API. I'm absolutely sure this is some stupid oversight on my part, I'm very green. Thank you again for your patience and responses.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2020 11:53 AM
In Allen's original reply he asked you, "Have you checked: requestObjFields.employee_id to see what it contains?" Your response makes me believe you misunderstood his question. He wants you to actually "check" what it is, not just assume what it is by implication.
In other words, do this:
gs.info(requestObjFields.employee_id);

- 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-04-2020 12:26 PM
You know what, now that you broke it down like this: I'm wondering if those fields aren't populated with the data until it builds it out the request type or something. Maybe I have this query too early when the fields are still 'blank' / null and haven't been filled with the information from the other side of the API yet.
Yup.
So I did the log to see 'gs.log("CDR" + requestObjFields.employee_id); and it returns as blank, nothing. Exactly like you and MWatkins said. So I guess that officially answers that. Unfortunately that just still doesn't give me a SOLUTION to my overall problem, just lets me know what I'm currently doing wrong, haha.
All I'm trying to do is build functionality into this API so that if there's more than 1 employee ID match (meaning duplicates) it spawns a Term Request for both and not just one. Guess that's a subject for another thread *sigh*. Thanks again! Seriously, wish there was a tipping system in this community, haha.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2020 12:36 PM
No worries. Keep asking questions! We've now created a permanent piece of documentation that can help many others.
Please ✅ Correct if this solves your issue and/or 👍 if Helpful