Scripted REST API: Data Processing after returning response

rahulyamgar
Tera Guru

Hello everyone,

I am working on the use case in which third party system is going to send us the JSON formatted data and I need to receive the data at ServiceNow and load the data to target tables using the data import function.

This is my approach: Accept the JSON payload->Create the DataSource using the staging table and associated transform->Load staging tables->run transforms. So, everything works as expected but I want to return response as soon as JSON payload is received, and then continue the data import using the functions/script includes.

How can I return the response and continue the data processing for scripted rest API.

 

Thanks in advance,

Rahul

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Rahul,

Basically this is the approach you can have;

I believe the data load, transform etc needs to happen asynchronous

1) create event

2) create script action associated to this event

3) in the scripted rest api script call the event and pass the entire json object you received

4) script action will then accept the value and do whatever you want to do i.e. insert data into import set table and then it would trigger the transform

5) Once you call the event using eventQueue() send the response to 3rd party about whatever status you want

Script Action:

https://docs.servicenow.com/bundle/newyork-platform-administration/page/administer/platform-events/r...

Sample Script: Rest API Script

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
	var reqbody = request.body.dataString;
	
	gs.eventQueue('event_name', null, reqbody,'');
	
	// send response
        return {
	"status": success
	};
	
	
})(request, response);

Script Action Script:

var dataString = event.parm1; // this will contain the entire request body sent

// now parse this json and do whatever is required

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

ServiceNow REST provides executeAsync()  method to handle your requirement.

Check below for more details

https://hi.service-now.com/kb_view.do?sysparm_article=KB0694711

https://hi.service-now.com/kb_view.do?sysparm_article=KB0716391

 

Regards,

Sachin

 

Hi Sachin,

It wouldn`t work as my use case is of inbound REST message. 

Thanks,
Rahul