How to send response in scripted rest API's
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2021 06:55 AM
Hi,
I have a requirement related to inbound integration , using scripted rest API's , import set table and transform map.
I am receiving two fields in the request payload from the third part tool to our servicenow.
They are hiting our scripted rest api. We have the scenario for update. We have created a API using put method.
now the requirement is that third party tool is hiting our scripted rest API and sending two fields in the request payload
{ "abc field" :"sys_id of the record" ,
" pqr field" : "value" }
Now firstly when the third party tool hits our api , it will store the data in import set table and after running the transform , we will send the data to target tables and update the related records in target table.
And in the response we have to provide the status code to the third party tool.
So how can we achieve this solution , I have created the scripted rest api , resource path and import set table.
But please help me with the logic that needs to be written in scripted rest API to send the response status to third party tool.
Thanks
Utkarsh
- Labels:
-
Integrations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2021 06:57 AM
In the REST API
add response.body=youjson object
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2021 06:59 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2021 07:03 AM
Hi,
Since you mentioned you will be inserting the record into staging table the transformation will be aysnchronous
So you can send some static response body which would say that it is success etc
example below
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var reqbody = request.body.dataString;
try{
var parser = new global.JSON();
var parsedData = parser.decode(reqbody);
// insert the record into staging table
var res = {};
res["status"] = "Success";
res["message"] = "Record submitted for transformation";
response.setBody(JSON.stringify(res));
}
catch(ex){
var res = {};
res["status"] = "Error";
res["message"] = ex.message;
response.setBody(JSON.stringify(res));
}
})(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
‎08-12-2021 07:42 AM
and after that we can a response as success or failure.
Thanks,
Utkarsh