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

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

Thanks Ankur. I would check and let you know. This is adding bit more complexity to the solution. Source can not create multiple/bulk entries in the import set staging table, so we created the Scripted REST API to receive the data and transform that to ServiceNow via import sets. I was just checking if we can acknowledge data receipt to the the source and continue processing Asynchronously. 

There are/must be options in JS to call function Asynchronously. But not able to get it executed.

I will try and keep you posted.

Thanks Again,

Rahul 

Hi Rahul,

I believe you wanted to process the data asynchronously which is coming from 3rd party.

even if it is 1 entry in import set per API call; we are not sure how much time it takes to process that single record depending on the field map, transform map scripts etc

If you could identify how much time it takes on average possibly you can make the script to wait for 5/10seconds and then send the response

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

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

@rahulyamgar 

Let me know if that answered your question or you need some more assistance.

If my answer solved your issue, please mark my answer as Correct & 👍Helpful so that others can benefit from similar question in future.

Regards
Ankur

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