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

Vrushali  Kolte
Mega Sage

Hello @Pravitha ,

 

You can use a Transform Map to transfer data from the staging table to the target table. If you don't already have a staging table, you can create a new one. When you receive a response from the REST API, you can parse it and use a GlideRecord query to populate the staging table. Once the data is in the staging table, the Transform Map will execute, allowing you to map fields from the staging table to the target table.

 

 

For more information on transform map you can refer below article :

https://docs.servicenow.com/bundle/tokyo-platform-administration/page/script/server-scripting/concep...

 

If my answer solves your issue, please mark it as Accepted ✔️ and Helpful 👍 based on the impact.

Hi Vrushali,

 

I already have a staging table. When third party send the update call, I get a new record in the staging table. But I'm not seeing the response/request body coming anywhere. I've mentioned the request body details (which was sent by third party) in my question above.

Pravitha_0-1716390265985.png

 

From your comment "When you receive a response from the REST API, you can parse it and use a GlideRecord query to populate the staging table. " - I don't see the response to parse. Which scripting method to be used here? 

Hi @Pravitha ,

 

Try this kinda code in Scripted REST API

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    var requestBody = request.body.data;
    var incNum = requestBody.update.calldata.ticketID;
    var grInc = new GlideRecord('incident');
    if (grInc.get(incNum)) {
        //take the value from the request body by dot walking. let say for an example, you are going to pull the value of previous workgroup value from additionalAttribute.
        var addAttr = requestBody.update.calldata.additionalAttribute;
        for (var i = 0; i < addAttr.length; i++) {
            var detailsName = addAttr[i].name;
            var details_content = addAttr[i]._content_;
            if (detailsName.includes('previousWorkgroup')) {
                grInc.ur_field_name = details_content;
            }
            if (detailsName.includes('additionalPhone')) {
                grInc.ur_field_name = details_content;
            }
        }
        grInc.update();
    }
})(request, response);

 

Regards,

Dhanraj.

Hi Dhanraj,

 

Thanks much for sharing the code snippet, really helpful!

However do I need to use staging table? or directly use Incident table?

Why I'm asking because I already have a staging table. When third party send the update call, I get a new record in the staging table. But I'm not seeing the response/request body coming anywhere. I've mentioned the request body details (which was sent by third party) in my question above.

Below is my staging table and new record inserted when third party sent the update call:

Pravitha_0-1716451503483.png