- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 02:43 AM
Hi Everyone,
I have created a custom Scripted REST API (POST Method), I am sending JSON data from scripted rest API to script include and updating work notes in incident. But I am facing in error handling.
Scripted REST API:
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body.dataString;
var ship = new PostSpecialComments().addComments(requestBody, response);
})(request, response);
Script Include:
var PostSpecialComments = Class.create();
PostSpecialComments.prototype = {
initialize: function() {},
addComments: function(request, response) {
// gs.info("Response = " + request);
var parsedData = JSON.parse(request);
var table = parsedData.object_name;
var sysId = parsedData.object_id;
var responseBody = {};
if (table == 'wm_order' || table == 'wm_task' || table == 'incident' || table == 'sn_customerservice_case' || table == 'pm_project') {
var rec = new GlideRecord(table);
rec.addQuery('sys_id', sysId);
rec.query();
if (rec.next()) {
// My logic of updating worknotes comes here.
responseBody.status = "Success";
responseBody.message = " ServiceNOW Operation performed successfully";
} else {
responseBody.status = "Failure";
responseBody.message = "org.mozilla.javascript.EvaluatorException: GlideRecord.addQuery() - incorrect value";
}
} else {
responseBody.status = "Failure";
responseBody.message = " org.mozilla.javascript.EvaluatorException: GlideRecord.addQuery() - invalid table name:" + table + "(sys_ws_operation.526056a71b46d910a662ece5624bcbd5.operation_script; line 17)";
}
response.setBody(responseBody);
},
type: 'PostSpecialComments'
};
I am getting this error in REST API explorer when I test:
{
"error": {
"message": "The undefined value has no properties.",
"detail": "ConversionError: The undefined value has no properties. (sys_script_include.b63853a247835a1061537e37536d4326.script; line 245)"
},
"status": "failure"
}
Basically, line 245 is response.setBody(responseBody). How can I solve my issue? Please help me in solving this issue.
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 04:33 AM
something like this
var PostSpecialComments = Class.create();
PostSpecialComments.prototype = {
initialize: function() {},
addComments: function(request, response) {
// gs.info("Response = " + request);
var parsedData = JSON.parse(request);
var table = parsedData.object_name;
var sysId = parsedData.object_id;
var responseBody = {};
if (table == 'wm_order' || table == 'wm_task' || table == 'incident' || table == 'sn_customerservice_case' || table == 'pm_project') {
var rec = new GlideRecord(table);
rec.addQuery('sys_id', sysId);
rec.query();
if (rec.next()) {
// My logic of updating worknotes comes here.
responseBody.status = "Success";
responseBody.message = " ServiceNOW Operation performed successfully";
} else {
responseBody.status = "Failure";
responseBody.message = "org.mozilla.javascript.EvaluatorException: GlideRecord.addQuery() - incorrect value";
}
} else {
responseBody.status = "Failure";
responseBody.message = " org.mozilla.javascript.EvaluatorException: GlideRecord.addQuery() - invalid table name:" + table + "(sys_ws_operation.526056a71b46d910a662ece5624bcbd5.operation_script; line 17)";
}
return JSON.stringify(responseBody);
},
type: 'PostSpecialComments'
};
Scripted REST API
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body.dataString;
var ship = new PostSpecialComments().addComments(requestBody, response);
var parsedData = JSON.parse(ship); // conver string to object
response.setBody(parsedData); // set that as response body
})(request, response);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 04:30 AM
you should set response body etc inside your scripted rest api
the script include should simply update/add the work notes
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 04:33 AM
something like this
var PostSpecialComments = Class.create();
PostSpecialComments.prototype = {
initialize: function() {},
addComments: function(request, response) {
// gs.info("Response = " + request);
var parsedData = JSON.parse(request);
var table = parsedData.object_name;
var sysId = parsedData.object_id;
var responseBody = {};
if (table == 'wm_order' || table == 'wm_task' || table == 'incident' || table == 'sn_customerservice_case' || table == 'pm_project') {
var rec = new GlideRecord(table);
rec.addQuery('sys_id', sysId);
rec.query();
if (rec.next()) {
// My logic of updating worknotes comes here.
responseBody.status = "Success";
responseBody.message = " ServiceNOW Operation performed successfully";
} else {
responseBody.status = "Failure";
responseBody.message = "org.mozilla.javascript.EvaluatorException: GlideRecord.addQuery() - incorrect value";
}
} else {
responseBody.status = "Failure";
responseBody.message = " org.mozilla.javascript.EvaluatorException: GlideRecord.addQuery() - invalid table name:" + table + "(sys_ws_operation.526056a71b46d910a662ece5624bcbd5.operation_script; line 17)";
}
return JSON.stringify(responseBody);
},
type: 'PostSpecialComments'
};
Scripted REST API
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body.dataString;
var ship = new PostSpecialComments().addComments(requestBody, response);
var parsedData = JSON.parse(ship); // conver string to object
response.setBody(parsedData); // set that as response body
})(request, response);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader