The CreatorCon Call for Content is officially open! Get started here.

Scripted REST Resource

madhusudanshett
Kilo Contributor

Hello Everyone

I have created the following Scripted REST Resource to get the information about Priority -1 Incidents:

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

  var table = 'incident',

  result_arr = [],

  gr = new GlideRecord(table);

  gr.addQuery('priority', 1);

  gr.query();

  while (gr.next()) {

  var result = {};

  result.short_description = gr.short_description;

  result.number = gr.number;

  result.state = gr.state;

  result.opened_at = gr.opended_at;

  result.location = gr.location.getDisplayValue();

  result.email= gr.email;

  result_arr.push(result);

  }

  return {

  "Result":result_arr,

  };

})(request, response);

When I try to get the response in REST API Explorer , I get the following message:

{
  "result": "",
  "error": {
  "message": "Cannot map object",
  "detail": "Cannot map object Check logs for error trace or enable glide.rest.debug property to verify REST request processing"
  },
  "status": "failure"
}

Please assist me to resolve this issue.

Thanks in advance.

17 REPLIES 17

HI@jhermann 

Maybe the below code can help you .

 

var gdtTest = new GlideDateTime();
var res = {
date: gdtTest.getDisplayValue()   // or other formater.
};
response.setBody(res);

Please mark my answer as correct and helpful based on Impact.

Nishant12
ServiceNow Employee
ServiceNow Employee

Convert your values to string values which should resolve the issue, I have seen the same error when i assign integer values to the property and send that as a response in JSON . 

newhand
Mega Sage

HI @madhusudanshett 

 

you don't need to return anything in the script restapi,

You just need to set the result_arr to the response .

 

response.setBody(result_arr)

 

Please mark my answer as correct and helpful based on Impact.