- 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-04-2022 07:16 AM
This line passes the producer name as the third param - this will always be the same value, right?
responseObj = addObjToObj(responseObj, variablesObject, inc.producer.name);
And this line uses it as a key - this will overwrite any existing property under this name.
function addObjToObj(parent, child, name) {
parent[name] = child;
I think that's why you get just one.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2022 09:45 AM
Thanks, I changed that to be the incident number and added an array as well to push the values. Its looking better (see above) but the variables for each incident just repeat themselves.
- 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);