Scripted REST Resource
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2016 09:18 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2017 11:19 PM
I encountered a similar error and resolved it by ensuring the returned result is a string (rather than a more complex object type). Russell's approach ensures a string result and so should work.
Madhusudan, in your case where you are trying to return an array of results, you'll want to ensure that each item in the array is a simple object containing only primitive types (e.g. string, number, boolean), or arrays and objects with similarly simple attributes. In your code, I would suggest replacing all of your dot-notation calls with calls to .getValue() or .getDisplayValue(). See below
...
while (gr.next()) {
var result = {};
result.short_description = gr.getValue('short_description');
result.number = gr.getValue('number');
result.state = gr.getDisplayValue('state');
result.opened_at = gr.getDisplayValue('opended_at');
result.location = gr.getDisplayValue('location');
result.email= gr.getDisplayValue('email');
result_arr.push(result);
...
Cheers,
Zac.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2018 06:44 AM
Hi I had the same problem and you indeed have to make sure it's a string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2019 06:10 PM
I was running into this error today and finally solved it by:
response.setBody(JSON.parse(JSON.stringify(ret)));
Hope this helps someone else

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2019 05:13 AM
I'd be curious to see what the output of the stringify was. This seems quite unnecessary to stringify then parse your object/array. Something else is at work. I suggest finding it and fixing it rather than working around it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2021 10:16 AM
Stumbled on this thread again today when I was seeing the same error again, and decided to find the root cause. Finding and fixing rather than working around an issue is great advice. However, it doesn't seem like a good fit in this case.
The stringify then parse approach is a performant way to "deep copy" objects in Javascript. It is also a quick and easy way to serialize objects for REST responses. The approach can have some unintended consequences with some data types, but I have never seen those issues in ServiceNow. Also, if the response is REALLY big (over 2^53 - 1 characters) you can hit issues with string limits.
Anyway, I took the time to find the root cause today. My issue was a result of including a GlideDateTime object reference in the response object. The OP likely hit the issue because of GlideElement objects in the response. In both cases, the stringify then parse approach would work well because it serializes the data for the response body. So, while it could cause issues, I still think it is a good solution. Using the getDisplayValue() or otherwise stringifying each element would also work.
Digging in more is almost always worth it and I think it led to a potentially valuable enhancement/bug fix to the setBody method of the response API or perhaps a new method like, setSerializedBody. It would be cool if it would automatically serialize the response. Right now nested objects (that are references) lead to errors from the setBody method. For example:
Scripted Rest Resource:
try {
var gdtTest = new GlideDateTime();
var res = {
date: gdtTest
};
response.setBody(gdtTest);
} catch (ex) {
err.setMessage(ex.message);
err.setDetail(JSON.stringify(ex, null, 2));
err.setStatus(500);
response.setError(err);
}
Response:
{
error: {
detail: "{↵ "message": "Cannot convert 2021-03-16 15:04:00 to org.mozilla.javascript.ScriptableObject (sys_ws_operation.abc123sysidremoved.operation_script; line 21)",↵ "fileName": "sys_ws_operation.abc123sysidremoved.operation_script",↵ "sourceName": "sys_ws_operation.abc123sysidremoved.operation_script",↵ "lineNumber": 21,↵ "name": "InternalError",↵ "stack": "\tat sys_ws_operation.abc123sysidremoved.operation_script:21 (process)\n\tat sys_ws_operation.abc123sysidremoved.operation_script:1\n",↵ "rhinoException": {}↵}"
message: "Cannot convert 2021-03-16 15:04:00 to org.mozilla.javascript.ScriptableObject (sys_ws_operation.abc123sysidremoved.operation_script; line 21)"
},
status: "failure"
}
To get the same error from the OP, and the error that brought me back here, you have to go through a Script Include:
Script Include
var SomeObject = Class.create();
SomeObject.prototype = {
initialize: function(objOrId) {},
getDateResponse: function(){
var gdtTest = new GlideDateTime();
var res = {
date: gdtTest
};
return res;
},
type: 'SomeObject'
}
Scripted Rest Resource:
try {
var someObject = new SomeObject();
var res = someObject.getDateResponse();
response.setBody(res);
} catch (ex) {
err.setMessage(ex.message);
err.setDetail(JSON.stringify(ex, null, 2));
err.setStatus(500);
response.setError(err);
}
Response:
{
...
"time":{
"seconds":51,
"xmlvalue":"1970-01-01 14:15:51",
"displayValue":"07:15:51",
"hourOfDayUTC":14,
"hourUTC":2,
"minutesUTC":15,
"hourOfDayLocalTime":7,
"hourLocalTime":7,
"minutesLocalTime":15,
"value":"1970-01-01 14:15:51",
"time":{
"seconds":51,
"xmlvalue":"1970-01-01 14:15:51",
"displayValue":"07:15:51",
"hourOfDayUTC":14,
"hourUTC":2,
"minutesUTC":15,
"hourOfDayLocalTime":7,
"hourLocalTime":7,
"minutesLocalTime":15,
"value":"1970-01-01 14:15:51",
"time":{
"seconds":51,
"xmlvalue":"1970-01-01 14:15:51"
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}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able glide.rest.debug property to verify REST request processing",
"message":"Cannot map object"
},
"status":"failure"
}
In this case there is a bigger issue. The end of the object has 2,284 closing curly braces, followed by a random string (the chrome console complains about an unexpected string in the response), then the error from the OP. It looks like a writer is caught in a loop where it doesn't close the object after the "value" property. Instead, it repeatedly nests another "time" object within the previous one.
That got longer than I expected, but hopefully it is valuable to someone else.