- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 08:47 AM
Response was not getting as expected in scripted rest API [Patch Rest API]
When I am using the below code, I am setting response as "Sysid" then it was perfectly fine...but when I was the send response as number and short description then I was getting error... what is the issue? how to set response as number and short description?
share the both codes.. please help me... It is a "Patch Rest API"
Below script, working fine [Response]
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var getID = request.pathParams.sysid;
var grt = request.body.data;
var gr = new GlideRecord('incident');
gr.addQuery('sys_id', getID);
gr.query();
if (gr.next()) {
gr.short_description = grt.short_description;
gr.contact_type = grt.contact_type;
gr.update();
return getID; //perfectly returning sysid
}
})(request, response);
Below script, Issue with response
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var getID = request.pathParams.sysid;
var grt = request.body.data;
var gr = new GlideRecord('incident');
gr.addQuery('sys_id', getID);
gr.query();
if (gr.next()) {
gr.short_description = grt.short_description;
gr.contact_type = grt.contact_type;
gr.update();
}
var n = gr.number;
response.setBody(n);
})(request, response);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2022 10:09 PM
You are not setting response in the right format, so create a array and store all the variables in it then pass the array into the response, then it will work perfectly.
Hope it helps, if so please mark my response as correct and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 10:31 AM
Hi, please just try to replace
var n = gr.number;
response.setBody(getID);
with
return {
"number": gr.number,
"description": gr.short_description
};
and re-test.
Hope it helps

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2022 10:19 AM
try this
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var getID = request.pathParams.sysid;
var grt = request.body.data;
var gr = new GlideRecord('incident');
gr.addQuery('sys_id', getID);
gr.query();
var n = "";
if (gr.next()) {
n = gr.getValue('number');
gr.short_description = grt.short_description;
gr.contact_type = grt.contact_type;
gr.update();
}
response.setBody(n);
})(request, response);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2022 07:36 AM
use this
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var response_body = {};
var getID = request.pathParams.sysid;
var grt = request.body.data;
var gr = new GlideRecord('incident');
gr.addQuery('sys_id', getID);
gr.query();
if (gr.next()) {
gr.short_description = grt.short_description;
gr.contact_type = grt.contact_type;
gr.update();
response_body["number"] = gr.getValue('number');
}
response.setContentType('application/json');
response.setStatus(200);
var writer = response.getStreamWriter();
writer.writeString(JSON.stringify(response_body));
})(request, response);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2022 10:09 PM
You are not setting response in the right format, so create a array and store all the variables in it then pass the array into the response, then it will work perfectly.
Hope it helps, if so please mark my response as correct and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2022 10:55 PM
Thanks
It worked..