- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2025 04:22 AM
I have issue with the below code in scripted rest api !!
var result = [];
var inc = new GlideRecord('incident');
inc.addEncodedQuery('caller_id=6816f79cc0a8016401c5a33be04be441');
inc.query();
while (inc.next()) {
var rBody = {};
rBody.caller = inc.caller_id.getDisplayValue();
rBody.short_description = inc.short_description.toString();
result.push(rBody);
}
response.setBody(JSON.stringify(result));
-------------
Error I am getting is :-
{ "error": { "message": "Script Evaluation Exception", "detail": "Cannot convert [{\"caller\":\"System Administrator\",\"short_description\":\"Demo SLA\"},{\"caller\":\"System Administrator\",\"short_description\":\"Demo Flow\"},{\"caller\":\"System Administrator\",\"short_description\":\"Demo creating Problem from incident\"},{\"caller\":\"System Administrator\",\"short_description\":\"Demo Before Insert BR\"},{\"caller\":\"System Administrator\",\"short_description\":\"asdfghjkljhngvfcdxsz\"},{\"caller\":\"System Administrator\",\"short_description\":\"Test\"},{\"caller\":\"System Administrator\",\"short_description\":\"First E-bonding V1\"},{\"caller\":\"System Administrator\",\"short_description\":\"Demo BR20251234567\"},{\"caller\":\"System Administrator\",\"short_description\":\"MYSQL has a ISSUE\"},{\"caller\":\"System Administrator\",\"short_description\":\"Demo\"},{\"caller\":\"System Administrator\",\"short_description\":\"Integration is failed !!\"},{\"caller\":\"System Administrator\",\"short_description\":\"Integration is failed !!\"},{\"caller\":\"System Administrator\",\"short_description\":\"This is Insert Operation\"},{\"caller\":\"System Administrator\",\"short_description\":\"6816f79cc0a8016401c5a33be04be441\"},{\"caller\":\"System Administrator\",\"short_description\":\"Demo\"},{\"caller\":\"System Administrator\",\"short_description\":\"Keyboard has a issue.\"},{\"caller\":\"System Administrator\",\"short_description\":\"ATF : Test1\"},{\"caller\":\"System Administrator\",\"short_description\":\"Change\"},{\"caller\":\"System Administrator\",\"short_description\":\"Demo007\"},{\"caller\":\"System Administrator\",\"short_description\":\"Issue with windows OS \"},{\"caller\":\"System Administrator\",\"short_description\":\"'p;oliku,jyhntgbrfvds\"},{\"caller\":\"System Administrator\",\"short_description\":\"Make SysId Same\"},{\"caller\":\"System Administrator\",\"short_description\":\"Demo Assignment Rule\"}] to org.mozilla.javascript.ScriptableObject (sys_ws_operation.fda23aaa475c3250051b392f316d43fa.operation_script; line 15)" }, "status": "failure" }
Please help !!
Do we have any document or video that will help
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2025 04:27 AM
The error happens because in a Scripted REST API in ServiceNow, the method response.setBody() only accepts a native JavaScript object or a simple string.
When you use JSON.stringify(), you are turning your object/array into a plain string, not an object.
ServiceNow then tries to handle that as a ScriptableObject, which causes the error you saw.
The fix is simple:
Just pass the JavaScript object directly into response.setBody(). ServiceNow will automatically convert it to JSON in the REST API response.
You can tweak the below code :-
var result = [];
var inc = new GlideRecord('incident');
inc.addEncodedQuery('caller_id=6816f79cc0a8016401c5a33be04be441');
inc.query();
while (inc.next()) {
result.push({
caller: inc.caller_id.getDisplayValue(),
short_description: inc.short_description.toString()
});
}
response.setBody({ incidents: result });
})(request, response);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2025 04:28 AM
You can follow the below documents also :
https://www.servicenow.com/docs/bundle/zurich-api-reference/page/integrate/custom-web-services/conce...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2025 04:27 AM
The error happens because in a Scripted REST API in ServiceNow, the method response.setBody() only accepts a native JavaScript object or a simple string.
When you use JSON.stringify(), you are turning your object/array into a plain string, not an object.
ServiceNow then tries to handle that as a ScriptableObject, which causes the error you saw.
The fix is simple:
Just pass the JavaScript object directly into response.setBody(). ServiceNow will automatically convert it to JSON in the REST API response.
You can tweak the below code :-
var result = [];
var inc = new GlideRecord('incident');
inc.addEncodedQuery('caller_id=6816f79cc0a8016401c5a33be04be441');
inc.query();
while (inc.next()) {
result.push({
caller: inc.caller_id.getDisplayValue(),
short_description: inc.short_description.toString()
});
}
response.setBody({ incidents: result });
})(request, response);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2025 04:28 AM
You can follow the below documents also :
https://www.servicenow.com/docs/bundle/zurich-api-reference/page/integrate/custom-web-services/conce...