How to pass the request body variables into target table?

Pravitha
Tera Contributor

I'm getting the below update request from third party to update the incident record. How can I pass the same to staging table and put it to the correct target?

I'm planning to use scripted REST API resource script, can you provide me a sample code to make this work so that I can update my incident record. Please suggest if any other way can this be achieved.

{
   "update":{
      "calldata":{
         "additionalAttribute":[
            {
               "_content_":"IN.Oracle.XXX.XXX",
               "name":"previousWorkgroup"
            },
            {
               "_content_":2,
               "name":"RawStatus"
            },
            {
               "_content_":"IN.Bridge.Monitoring",
               "name":"AssignedWorkGroup"
            },
            {
               "_content_":"IN65",
               "name":"site"
            },
            {
               "_content_":"+9100000196789",
               "name":"additionalPhone"
            },
            {
               "_content_":"TestUser",
               "name":"customerFirstName"
            },
            {
               "_content_":"Name",
               "name":"customerLastName"
            },
            {
               "_content_":"testusername@abc.net",
               "name":"customerEmail"
            },
            {
               "_content_":"A7488812",
               "name":"customerGID"
            },
            {
               "_content_":false,
               "name":"EndUserIncomingContact"
            },
            {
               "_content_":"yyyy-MM-ddTHH:mm:ssZ",
               "name":"dateTimeFormat"
            },
            {
               "_content_":false,
               "name":"IsReferenceCopy"
            },
            {
               "_content_":"Fault",
               "name":"callType"
            }
         ],
         "extTicketID":"INC000002",
         "comment":"Test 1\nTransfer to internal group",
         "time":{
            "timestamp":"2024-05-03T11:09:50Z"
         },
         "ticketID":"INC02200008",
         "status":"Work in Progress"
      },
      "problemDescription":{
         "_content_":"",
         "addToCustomerdescription":false
      },
      "header":{
         "transferMaster":false,
         "dstApplicationID":"XXXX-SNOW-C",
         "messageID":140456381,
         "srcApplicationID":"XXX-SNOWGL-C"
      }
   }
}

8 REPLIES 8

Hi @Pravitha ,

 

If you already have staging table to create incident records, then go with it.

 

Additional suggestion:

Kindly avoid granting the direct access to the incident table, if that integrated source is accessible by everyone in your organization. Better have an approval process or validation before creating a record in the incident table.

 

Regards,

Dhanraj.

KevinBellardine
Kilo Sage

Hey @Pravitha 

 

In your scripted API you can access the requestBody by using request.body.data or request.body.dataString. The first will try to give you the JSON itself, but if you're running into issues you can use dataString and then parse it back to JSON.

 

From here you have a few options. I would just iterate through the object in your script, or a script include, and use gliderecord to create a record in your staging table. If you've got access to it and you're comfortable with flow designer you could also build a subflow, which would help you with the parsing but requires more setup.

Hi Kevin,

 

Your reply really helps!

Could you also please provide a sample code on how to access the requestBody by using request.body.data or request.body.dataString and use gliderecord to insert to my staging table?

mattsentara
Tera Contributor

Here is some sample code from a similar situation using a Scripted REST Resource. In this case we are iterating through multiple rows in the response body. Each row gets passed as an array to a Script Include to create the records in the import table. We then run the transform from the import table to the destination. I'm sure there is a cleaner way to accomplish this, but hopefully this is helpful.

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
	
	var writer = response.getStreamWriter(); //Returns the ResponseStreamWriter for this response, allowing you to write directly to the response stream
	var hdrs = {};
	hdrs['Content-Type'] = 'application/json';
	hdrs['Accept'] = 'application/json';
	response.setHeaders(hdrs);
	
	try {
		// Start payload processing
		response.setStatus(200);
		var req_body = request.body;

		while(req_body.hasNext()) {
			var payload = req_body.nextEntry();
			var DataArr = payload.row;
			var APIService = new APIService();

			var resultsArr = APIService.createDataRec(DataArr);
			var responseBody = {};
			responseBody.result = resultsArr;
			//gs.log(JSON.stringify(responseBody), 'API definition - Data SUCCESS');
			writer.writeString(JSON.stringify(responseBody));
		}
	} catch(err_obj) {
		//gs.log(JSON.stringify(err_obj), 'API definition - Data ERROR');
		writer.writeString(JSON.stringify(err_obj));
	}

})(request, response);