Can we send multiple records to Service Now within a single REST request?

Kaushik Muley1
Giga Contributor

As per the requirement, the third party tool is supposed to send multiple records in a single REST request.
Can Service Now import set API consume the multiple record JSON to create/update records on target table?

If yes, what should be the format of JSON string?

As per the initial analysis, I found the wiki article on JSONv2, but it does not seem to be working with REST request.

1 ACCEPTED SOLUTION

Yes, a scripted REST API is going to be a better way to go in this case (over a processor). The biggest advantage is that you can use versioning so one app can continue to use v1 (perhaps with no URI parameters) while another app can use v2 (with two URI parameters) simultaneously. Processors cannot do that. You need to maintain two separate processors which can be cumbersome. There are other advantages as well.



Scripted REST APIs


Check Episode 23 here: TechNow Episode List  


View solution in original post

10 REPLIES 10

I would make a Processor instead, where you can send the JSON as a parameter.



Then use the JSONParser and create the appropriate records.



Processors


Tutorial 4 Creating a Custom Processor - ServiceNow Wiki


JSONParser - ServiceNow Wiki


Yes, a scripted REST API is going to be a better way to go in this case (over a processor). The biggest advantage is that you can use versioning so one app can continue to use v1 (perhaps with no URI parameters) while another app can use v2 (with two URI parameters) simultaneously. Processors cannot do that. You need to maintain two separate processors which can be cumbersome. There are other advantages as well.



Scripted REST APIs


Check Episode 23 here: TechNow Episode List  


Is there any way where we can get the data from header and then enter the data into the table.I am able to do it for a single record.is there a way where I can get the header length and add data dynamically for multiple records.

spandpot
Giga Contributor

Hello Kaushik,



I had a similar requirement, where I had to create multiple records in a table from a UI page,
Can you try something like this:


JSON.stringify an array of arrays containing your record objects and POST to your script include/processor. In the processor, glide to incident table (required table), iterate through this array and create records. Here is an example with a processor:


In the client script:


records = [[{impact:'1',short_description:'Test'}],[{impact:'2',short_description:'Testing}]];
var data = JSON.stringify(records);


postData();


postData = function(){


$http({


  method:'POST',


  url: '/processor.do'


  headers: {'Content-Type': 'application/json'},


  data:data


  }).success(function(data,status){


  console.log('Data saved successfully');


  }).error(function(error,status){


  console.log('Error in saving data.');


  });


}


In the processor:


var inputStream = g_request.getInputStream();


var sb = GlideStringUtil.getStringFromStream(inputStream);


var parser = new JSONParser();


var data = parser.parse(sb);


for(var i=0; i< data.length; i++){


  for(var j=0;j<data[i].length;j++){


  var impact = data[i][j].impact;


  var short_description = data[i][j].short_description;


  var gr= new GlideRecord('incident');


  gr.initialize();


  gr.short_description = short_description;


  gr.impact = impact;


  gr.insert();


  }


}


If it is a script includes, similar logic will work, just take care of the syntax as per wiki. Hope this helps!


PS - Please mark Helpful, Like, or Correct Answer if applicable.


kaushalpragnya
Kilo Explorer

hi I tried the above scenario with the scripted REST API


To import two records [{"country":"US","company":"abc"},{"country":"INDIA","company":"def"}]in to staging table I followed the below way.



(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {



var country;
var gr;
var company;
var employeenumber;
var requestBody = request.body;
var requestData = requestBody.data; //May be an array or a single object
if (requestData instanceof Array)
{
      for(i=0;i<requestData.length;i++)
  {
            country = requestData[i].country; // 'user1'
            company = requestData[i].company;// '1234'
            employeenumber=requestData[i].employeenumber;
   
gr=new GlideRecord('u_samplerest');   //u_samplerest is the staging table
    gr.initialize();  
    gr.country=country;
          gr.company=company;
          gr.employee_number=employeenumber;
gr.insert();
  }
}
else
{
            country = requestData.country; // 'user1'
            company = requestData.company; // '1234'
    employeenumber=requestData.employeenumber;
    gr=new GlideRecord('u_samplerest');
    gr.initialize();  
    gr.country=country;
          gr.company=company;
gr.employee_number=employeenumber;
gr.insert();
  }
   
})(request, response);




[{"country":"US","company":"abc"},{"country":"INDIA","company":"def"}]//