Scripted REST API returning only 1 result

JTM201
Tera Expert

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);

 

1 ACCEPTED SOLUTION

JTM201
Tera Expert

 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);

View solution in original post

12 REPLIES 12

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi JTM201,

Try adding .toString() when pushing to an array.

inc_number.push(inc.task.number.toString());

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. 

Simon Christens
Kilo Sage

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());

find_real_file.png

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.