- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2022 01:10 PM
My scripted REST API only returns one result. When I run as a background script and stringify the object, I see all of the results. Im not limiting the results at all in the API so there must be something in the code.
EDIT: I believe I figured this out. I need to clear the variableName and variablesObject at the end of the loop so when it loops back through with another incident, it has an empty variables object. Thanks everyone. Working code below:
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var variableName,
i,
incidentVariables = [],
catalog_sys,
inc,
variablesObject = {},
inc_number = [],
arr = [],
responseObj = {}; //The object that should ultimately be returned.
catalog_sys = request.getHeader('catalog');
inc = new GlideRecord('sc_item_produced_record');
inc.addQuery('producer', catalog_sys);
inc.query();
while (inc.next()) {
inc_number.push(inc.task.number);
}
for (i = 0; i < inc_number.length; i++) {
var inc_table = new GlideRecord('incident');
inc_table.addQuery('number', inc_number[i]);
inc_table.query();
while (inc_table.next()) {
incidentVariables = inc_table.variables;
//variablesObject["number"] = inc_number[i].toString();
for (variableName in incidentVariables) {
if (incidentVariables.hasOwnProperty(variableName) && incidentVariables[variableName]) { //Make sure the property exists and isn't null or unknown.
variableName = variableName.toString(); //Make sure we're all proper strings here.
variablesObject[variableName] = incidentVariables[variableName].getDisplayValue().toString();
}
}
}
responseObj = addObjToObj(responseObj, variablesObject, inc_number[i].toString());
arr.push(responseObj);
variableName = "";
variablesObject = {};
}
return arr;
//Helper function below
function addObjToObj(parent, child, name) {
parent[name] = child;
return parent; //Note: Does not break pass-by-reference, because we're declaring a new object on each loop on line 39 above.
}
})(request, response);
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2022 11:34 AM
I believe I figured this out. I need to clear the variableName and variablesObject at the end of the loop so when it loops back through with another incident, it has an empty variables object. Thanks everyone. Working code below:
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var variableName,
i,
incidentVariables = [],
catalog_sys,
inc,
variablesObject = {},
inc_number = [],
arr = [],
responseObj = {}; //The object that should ultimately be returned.
catalog_sys = request.getHeader('catalog');
inc = new GlideRecord('sc_item_produced_record');
inc.addQuery('producer', catalog_sys);
inc.query();
while (inc.next()) {
inc_number.push(inc.task.number);
}
for (i = 0; i < inc_number.length; i++) {
var inc_table = new GlideRecord('incident');
inc_table.addQuery('number', inc_number[i]);
inc_table.query();
while (inc_table.next()) {
incidentVariables = inc_table.variables;
//variablesObject["number"] = inc_number[i].toString();
for (variableName in incidentVariables) {
if (incidentVariables.hasOwnProperty(variableName) && incidentVariables[variableName]) { //Make sure the property exists and isn't null or unknown.
variableName = variableName.toString(); //Make sure we're all proper strings here.
variablesObject[variableName] = incidentVariables[variableName].getDisplayValue().toString();
}
}
}
responseObj = addObjToObj(responseObj, variablesObject, inc_number[i].toString());
arr.push(responseObj);
variableName = "";
variablesObject = {};
}
return arr;
//Helper function below
function addObjToObj(parent, child, name) {
parent[name] = child;
return parent; //Note: Does not break pass-by-reference, because we're declaring a new object on each loop on line 39 above.
}
})(request, response);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2022 08:52 PM
Hi JTM201,
Try adding .toString() when pushing to an array.
inc_number.push(inc.task.number.toString());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2022 06:50 AM
Thanks, but the issue is that the for loop creates an object for each record when I need it to create one object containing all records.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2022 10:09 PM
Are you running it as the same user ?
When running it with a background script its probably as admin right.
When calling the API from outside / elsewhere is it then still as an admin ?
Try logging out the query for the incident to ensure that you are not hit by the Business rule "Incident query"
var inc_table = new GlideRecord('incident');
inc_table.addQuery('number', inc_number[i]);
inc_table.query();
gs.log(inc_table.getEncodedQuery());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2022 06:52 AM
I am an admin and running as myself. The issue is that the for loop creates an object for each record when I need it to create one object containing all records.