- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 08:20 AM
Hi All,
I am working on a scripted rest API inbound integration with third party tool.
For that I have create a scripted rest API and a script include as below.
Scripted REST API:
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var requestBody = request.body;
var requestData = requestBody.data;
var requestObj;
if(requestData instanceof Array){
for(requestObj in requestData){
new InsertIncident().ProcessInsertIncident(requestData[requestObj], response);
}
}else{
new InsertIncident().ProcessInsertIncident(requestData, response);
}
})(request, response);
Script Include:
var InsertIncident = Class.create();
InsertIncident.prototype = {
initialize: function() {},
ProcessInsertIncident: function(requestData, response) {
var validJSON = this._checkJSONValidity(requestData);
var reqeustJSONParser = JSON.parse(validJSON);
var responseBody = {};
if (validJSON == true) {
var inc = new GlideRecord('incident');
inc.addQuery('correlation_id', reqeustJSONParser.id);
inc.query();
if (inc.next()) {
inc.caller_id = reqeustJSONParser.caller_id;
inc.short_description = reqeustJSONParser.short_description;
inc.description = reqeustJSONParser.description;
inc.work_notes = reqeustJSONParser.work_notes;
inc.category = reqeustJSONParser.category;
inc.update();
responseBody.Message = 'Incident is Updated';
} else {
inc.initialize();
inc.caller_id = reqeustJSONParser.caller_id;
inc.short_description = reqeustJSONParser.short_description;
inc.description = reqeustJSONParser.description;
inc.comments = reqeustJSONParser.comments;
inc.correlation_id = reqeustJSONParser.id;
inc.insert();
responseBody.Message = 'Incident is Created';
responseBody.Number = inc.number;
}
response.setBody(responseBody);
}
},
_checkJSONValidity: function(validJSON) {
try {
JSON.parse(validJSON);
} catch (e) {
return false;
}
return true;
},
type: 'InsertIncident'
};
And I am not able to create/update incidents. In case if I change my script include as below, I am receiving the data. But I want to parse the JSON data before creating/updating.
var InsertIncident = Class.create();
InsertIncident.prototype = {
initialize: function() {},
ProcessInsertIncident: function(requestData, response) {
var responseBody = {};
var inc = new GlideRecord('incident');
inc.addQuery('correlation_id', reqeustJSONParser.id);
inc.query();
if (inc.next()) {
inc.caller_id = reqeustJSONParser.caller_id;
inc.short_description = reqeustJSONParser.short_description;
inc.description = reqeustJSONParser.description;
inc.work_notes = reqeustJSONParser.work_notes;
inc.category = reqeustJSONParser.category;
inc.update();
responseBody.Message = 'Incident is Updated';
} else {
inc.initialize();
inc.caller_id = reqeustJSONParser.caller_id;
inc.short_description = reqeustJSONParser.short_description;
inc.description = reqeustJSONParser.description;
inc.comments = reqeustJSONParser.comments;
inc.correlation_id = reqeustJSONParser.id;
inc.insert();
responseBody.Message = 'Incident is Created';
responseBody.Number = inc.number;
}
response.setBody(responseBody);
},
type: 'InsertIncident'
};
Please assist and correct me if I am wrong.
Thanks,
Priya.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 10:50 AM
Hello priya ,
You can add try catch for it in your function while you are stringifying it like below
_checkJSONValidity: function(validJSON)
{
try
{
return JSON.stringify(validJSON);
}
catch(e) { gs.info('parsing error'+e); }
}
Please mark my response as correct of it helped you

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 08:27 AM
Hi,
From what you've provided, this could have been achieved with the Import Set API rather than building a SRAPI
Import Set API - Goodbye SRAPIs? - Developer Community - Article - ServiceNow Community
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 08:31 AM
Hi Kieran,
This is just a sample JSON which I am using here. But the actual JSONs will have many fields and few conditions as well, so that is why we are going with SR API.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 08:41 AM
Hi priyarao,
Why are you parsing returned true/false value here ?
var validJSON = this._checkJSONValidity(requestData);
var reqeustJSONParser = JSON.parse(validJSON);
I think this should be as follows;
var reqeustJSONParser = JSON.parse(requestData);
Hopefully, this will help you debug your code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 08:50 AM
Hi Muhammad,
I have updated as below but still facing the same issue. also removed true for the if condition.
var reqeustJSONParser = JSON.parse(requestData);