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.

How to display table records in Scripted REST API

MJ32
Tera Contributor

Hi, I have been working on the ServiceNow platform for a while and now moved onto the development side (gone though the online training). However I am a little stuck and hoping for help or guidance.

I have a script that works in Scripts - Background module. It basically displays data from a table:

var tableRecords = new GlideRecord('u_cisco_servers');
    
tableRecords.orderBy('location.name');
tableRecords.addActiveQuery();
tableRecords.query();
    
var message = gs.print('---> ROW COUNT: ' + tableRecords.getRowCount());
    
while (tableRecords.next()){
gs.print(tableRecords.getValue('u_ip_address') + ':' + tableRecords.u_membership_group);
}

This works as expected:

find_real_file.png

However when I add this script the the Scripted REST API module, it does not iterate through the rows and provide the same information via REST:

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

var tableRecords = new GlideRecord('u_cisco_servers');
    
 tableRecords.orderBy('location.name');
 tableRecords.addActiveQuery();
 tableRecords.query();
    
 var message = gs.print('---> ROW COUNT: ' + tableRecords.getRowCount());
    
while (tableRecords.next()){

var responseObj ={
"Table Name" : tableRecords.getTableName(),
"Display Value" : tableRecords.getDisplayValue(),
"Row Count" : tableRecords.getRowCount(),
"IP ADDRESS": tableRecords.getValue('u_ip_address'),
"MEMBERSHIP GROUP": tableRecords.getValue('u_membership_group'),
};

response.setBody(responseObj);

})(request, response);

This is the response:

{
    "result": {
      "Table Name": "u_cisco_servers",
      "Display Value": "Created 31-10-2019 16:26:14",
      "Row Count": 2,
      "IP ADDRESS": "192.168.1.2",
      "MEMBERSHIP GROUP": "Test"
    }
  }

I have looked online and through the documentation and can't seem to find the right information.

Any help would be appreciated.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

basically you need to declare an array and then push each json object in that array; the final script would look something like this

what you are doing is; as soon as you are entering into the while loop you are forming the json object and returning the response object

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

var arr = [];

var tableRecords = new GlideRecord('u_cisco_servers');
    
 tableRecords.orderBy('location.name');
 tableRecords.addActiveQuery();
 tableRecords.query();
    
 var message = gs.print('---> ROW COUNT: ' + tableRecords.getRowCount());
    
while (tableRecords.next()){

var responseObj ={
"Table Name" : tableRecords.getTableName(),
"Display Value" : tableRecords.getDisplayValue(),
"Row Count" : tableRecords.getRowCount(),
"IP ADDRESS": tableRecords.getValue('u_ip_address'),
"MEMBERSHIP GROUP": tableRecords.getValue('u_membership_group'),
};

arr.push(responseObj);

}

response.setBody(arr);

})(request, response);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

3 REPLIES 3

Pratiksha Kalam
Kilo Sage

Hi,

Go through below link for scripted REST API,

https://docs.servicenow.com/bundle/newyork-application-development/page/integrate/custom-web-service...

 

Regards,

Pratiksha

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

basically you need to declare an array and then push each json object in that array; the final script would look something like this

what you are doing is; as soon as you are entering into the while loop you are forming the json object and returning the response object

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

var arr = [];

var tableRecords = new GlideRecord('u_cisco_servers');
    
 tableRecords.orderBy('location.name');
 tableRecords.addActiveQuery();
 tableRecords.query();
    
 var message = gs.print('---> ROW COUNT: ' + tableRecords.getRowCount());
    
while (tableRecords.next()){

var responseObj ={
"Table Name" : tableRecords.getTableName(),
"Display Value" : tableRecords.getDisplayValue(),
"Row Count" : tableRecords.getRowCount(),
"IP ADDRESS": tableRecords.getValue('u_ip_address'),
"MEMBERSHIP GROUP": tableRecords.getValue('u_membership_group'),
};

arr.push(responseObj);

}

response.setBody(arr);

})(request, response);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

MJ32
Tera Contributor

Thank you both, I managed to figure it out eventually with the array.