Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Query multiple table in Scripted REST Api

DM8
Kilo Contributor

I am trying to query two different tables and want to print their results in the response body.

First I am query the first table, so I want all the record's response then ll the record's response from second table. Through the below script when I am querying one table I am getting the response, but on querying two table there is no response. Can anyone please help me on this.

Thank

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var writer = response.getStreamWriter();
    hdrs = {};
    var gr = new GlideRecord('computer');
    var gr2 = new GlideRecord('rel');

    hdrs['Content-Type'] = 'application/json';
    response.setStatus(200);
    response.setHeaders(hdrs);

    //query first table
    gr.addEncodedQuery('u_it_id!=NULL^os!=^u_it_type!=Network');
    gr.query();

    // trying to set the response body for 1st table
    writer.writeString("{\"results\":[");
    while (gr.next()) {
        var body = {};
        body.name = gr.name + '';
        writer.writeString(global.JSON.stringify(body));
        if (gr.hasNext()) {
            writer.writeString(",");
        }
    }
    writer.writeString("]}");

    //query second table

    gr2.addEncodedQuery('u_class_name!=appl');
    gr2.query();
  
  // trying to set the response body for 2nd table
    writer.writeString("{\"results\":[");
    while (gr2.next()) {
        var bodysec = {};
        bodysec.name = gr2.childname + '';
        writer.writeString(global.JSON.stringify(bodysec));

        if (gr2.hasNext()) {
            writer.writeString(",");
        }
    }

    writer.writeString("]}");

})(request, response);
1 ACCEPTED SOLUTION

Hi,

you need to use same object and push the key values for the respective while loops

I would encourage to use array of json object as it makes no sense in repeating the same json key i.e. name

try this and test once

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

var arr = []

var gr = new GlideRecord('computer');
gr.addEncodedQuery('u_it_id!=NULL^os!=^u_it_type!=Network');
gr.query();
while (gr.next()) {
var obj = {};
obj.name = gr.name + '';
arr.push(obj);
}

var gr2 = new GlideRecord('rel');
gr2.addEncodedQuery('u_class_name!=appl');
gr2.query();
while (gr2.next()) {
var bodysec = {};
bodysec.name = gr2.childname + '';
arr.push(bodysec);
}

response.setBody(arr);

response.setContentType('application/json');
response.setStatus(200);

})(request, response);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Allen Andreas
Tera Patron

Hello,

There seem to be a few threads from you regarding this, are they still relevant or should they be deleted?

Similar issue: https://community.servicenow.com/community?id=community_question&sys_id=2c3910cfdb4f5c10d5c4d9d96896...

Similar issue again: https://community.servicenow.com/community?id=community_question&sys_id=fd410bb2db075c1011762183ca96...

Let us know?

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Allen,

They are still relevant, and this a different issue that I am facing

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Is there any relationship between those 2 queries?

if not then you can capture the result in same object

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

var body = {};

I used the same body objects in both the while loops, but still didnot give any response