Response to Scripted REST API Payload

balaji_prusty1
Giga Guru

Hi All,

 

I have created a staging table using web service for my ServiceNow Integrtion. Created below Scripted Rest API to receive the payload from a third party. It should create an incident using a staging table and respond to the Incident number in the response body. I am trying to capture the payload data into the staging table and create an incident using a transform map. There seems delay in b/w two entries and it is not responding to the incident number in the response body. But when I compare the created date/time there is no delay. Not sure I am missing something here. Here is my Scripted API code. Can someone help with this?

=============================================

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
 
 var requestBody = request.body;
var rquestString = requestBody.dataString;
var parsedBody = JSON.parse(rquestString);
 
    // Extract necessary data from the request body
var shortDescription = parsedBody.short_description || null;
var contact_type = "event";
var u_choice_2 = "Cyber Security";
var category = parsedBody.category;
var description = parsedBody.description;
 var caller = parsedBody.caller;
var impact = parsedBody.impact;
    var urgency = parsedBody.urgency;
var state = 1;
var assignment_group = parsedBody.assignment_group;
var correlation_id = parsedBody.corrleation_id;
 
var gr = new GlideRecord('x_*********_integration'); // Staging table name
    gr.initialize();
    gr.u_short_description = shortDescription;
    gr.u_description = description;
    gr.u_caller = caller;
    gr.u_urgency = urgency;
gr.u_impact = impact;
gr.u_choice_2 = u_choice_2;
gr.u_channel = contact_type;
gr.u_state = state;
gr.u_assignment_group = assignment_group;
gr.u_category = category;
gr.u_correlation_id = correlation_id;
 gr.insert();  // Insert the record
 
var globalUtils = new global.MyGlobalScopeUtils();
globalUtils.sleep(10000);  // added some delay to capture incident number
 
// To get incident number
var incno;
var incRec = new GlideRecord('incident');
incRec.addQuery('sys_id',gr.sys_target_sys_id);  // pass the ref sys_id from staging table table to get inc number
incRec.query();
if(incRec.next())
{
incno = incRec.number;
}
 
// Construct response
    var responseBody = {
        "status": "success",
        "message": "Incident created successfully",
        "incident_number": incno,    // incident number to respond
"Staging Number": gr.number,// Return the incident number for reference
 
    };
})(request, response);
 
============================
 
Thanks
Balaji
5 REPLIES 5

Weird
Mega Sage

It's a bit weird to use scripted rest api and then a staging table. What are your coalesce fields there?
Could you not just first query a incident and if none is found then create a new one directly in the scripted rest api?

Hi

 

I have created a correlation ID in the transform map which will hold the customer's "incident number". So next time if it finds the same number then it will update the staging table and the same will be reflected in the corresponding incident which is working fine.

 

I am not querying the incident. It depends on the staging table entry. If new record then it will create a new incident else will be updated.

But you could do it directly in the scripted rest API as well.
query for the correlation ID value. If found, update, else insert.
If you're using a staging table, you could just tell the other party to send the rest message there?


If you really want to use the staging table and can't have the 3rd party send a message there then you could perhaps create outbound rest API for your instance itself. It's a bit of unnecessary extra compared to the first option I gave, but that way when you send the rest message your script will continue after a response has been received. The response would contain the target sys_id which you could then return.
This would also allow you to filter out stuff in the scripted rest api if you don't want to let everything through.

Yousaf
Giga Sage

Hi @balaji_prusty1 

Please check if these can help.
How to make use of gs.sleep() in a scoped application scripts 

gs.sleep() in business rule? 

 

Mark Correct and Helpful if it helps.


***Mark Correct or Helpful if it helps.***