- 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:53 AM
Try placing this statement inside if(validJSON)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 08:56 AM
Hi Muhammad,
Updated the code as below, but still facing the issue.
var InsertIncident = Class.create();
InsertIncident.prototype = {
initialize: function() {},
ProcessInsertIncident: function(requestData, response) {
var validJSON = this._checkJSONValidity(requestData);
var reqeustJSONParser = JSON.parse(requestData);
var responseBody = {};
if(validJSON) {
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'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 09:02 AM
I was saying that try something like below
if(validJSON){
var reqeustJSONParser = JSON.parse(requestData);
}
// Also try using
_checkJSONValidity: function(checkJSON) {
try {
JSON.parse(checkJSON);
} catch (e) {
return false;
}
return true;
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 09:00 AM
hello
try using this in your parsing function
_checkJSONValidity: function(validJSON) {
return JSON.stringify(validJSON);
},
Rest everything keep as is and try which is and continue what you were doing in the rest of the code and try
ProcessInsertIncident: function(requestData, response) {
var validJSON = this._checkJSONValidity(requestData);
var reqeustJSONParser = JSON.parse(validJSON);
please mark my answer correct if it helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2022 09:16 AM
Thank you Mohith.
It is working fine now. Incident is getting created and I am able to update it too.