Search code examples
javascriptservicenowservicenow-rest-api

How to get all data from table using scripted rest api servicenow?


I am new to servicenow and trying to get all the data using scripted rest api but getting blank result.

Code:

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response)
{
    //var data = [];
    var records = new GlideRecord("table_name");
    var result = records.query();

response.setBody(result);
})(request, response);

I am expecting to get whole table data from the call, could you please help how to do that.


Solution

  • ServiceNow already has a great out-of-box API called the Table API that can probably do everything you need, so definitely check that out first, but if you find that won't do everything you need for some reason, keep reading.

    I should mention here at the outset it’s almost never recommended to send every record in an entire table over REST like this. Maybe you simplified your example for posting here and this is a moot point, but if not I highly recommend you query for precisely the subset of records you need. This will keep the user interactions as performant as possible and make for happier users and less stressed out admins.

    The problem you’re having is the response body from your Scripted REST API will be serialized into a JSON string before being sent back to the client, and GlideRecord objects aren’t really native JavaScript objects, they’re arcane witch magic half-Java/half-JavaScript objects that won’t handle the serialization very well at all, so you’ll need to do a little more work to prepare the data.

    (function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
      var gr = new GlideRecord('table_name');
      gr.addQuery() // Some appropriate filter here for better performance
      gr.query();
    
      var results = [];
    
      while (gr.next()) {
        results.push({
          first_field: gr.getValue('first_field'),
          second_field: gr.getValue('second_field'),
          third_field: gr.getValue('third_field')
        });
      }
      
      response.setBody(results);
    })(request, response);
    

    If you absolutely need all the fields (though, again, for performance reasons I don’t recommend this; you should always send just the subset of fields you absolutely need), you could use GlideRecord's getFields method like this:

    (function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
      var results = [];
      var result;
      var fields;
      var field;
      var i;
    
      var gr = new GlideRecord('table_name');
      gr.addQuery() // Some appropriate filter here for better performance
      gr.query();
    
      while (gr.next()) {
        fields = gr.getFields();
        result = {};
     
        for (i = 0; i < fields.size(); i++) {
          field = fields.get(i).getName();
          result[field] = gr.getValue(field);
        }
    
        results.push(result);
      }
      
      response.setBody(results);
    })(request, response);
    

    I highly recommend learning the GlideQuery API in addition to GlideRecord. GlideQuery has the advantage for this use case (and many others) that the objects it produces are native JavaScript objects and can be immediately serialized into JSON strings, which greatly simplifies things.

    (function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
      var results = new GlideQuery('table_name')
        .where() // Some appropriate filter here for better performance
        .select('first_field', 'second_field', 'third_field')
        .reduce(function (records, record) {
          return records.concat(record);
        }, []);
      
      response.setBody(results);
    })(request, response);
    

    And here’s an example that pulls all the fields, if that’s really what you need.

    (function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
      var schema = Schema.of('table_name', ['*']);
      var fields = Object.keys(schema.table_name);
    
      var results = new GlideQuery('table_name')
        .where() // Some appropriate filter here for better performance
        .select(fields)
        .reduce(function (records, record) {
          return records.concat(record);
        }, []);
      
      response.setBody(results);
    })(request, response);
    

    Welcome to the ServiceNow developer community! Hope this helps. Cheers!