Can I insert more than one import set record using/consuming the native REST API for import sets?

Arturo3
Tera Contributor

I'm facing an scenario where an organization wants to use a REST web service living in Servicenow to create more than one asset in one call. Analyzing the capabilities of the REST API for import sets, I understand only import set record can be created through the "native" REST API.

I even created a javascript application that calls the REST API and creates one import set record but I haven't been able to create more than one import set record (two assets) in the same call. Any feedback will be really appreciated.

6 REPLIES 6

Arturo3
Tera Contributor

Trying a different approach


LaurentChicoine
Tera Guru

The native Rest API only allows to import a single record per call.



We had similar requirement and chose to go with SOAP as it natively supported that method (insertMultiple)



See: Inserting multiple records using insertMultiple


That's correct insertMultiple is NOT available for REST. However, it is available for SOAP.


davidmcdonald
Kilo Guru

Hi Jose,



We faced this issue during a rather complicated integration.



SHORT ANSWER


No, you can't use the Import API for multiple import rows sadly. Unfortunately, it's not a simple task.



METHOD 1 - Custom Scripted SOAP / REST


Using a custom-written soap & rest endpoint that you'd create yourself, you can have it read a payload (XML, JSON, whatever your favourite flavor is) and write script to interpret it, and return a result.



find_real_file.png



e.g. lets assume your POST body parameter is called 'payload' and is a JSON array of results, and the response is in JSON


var responseMessages = {}; //New messages go in here


var payloadParsed = JSON.parse(request.payload); //JSON is native to basic Javascript, and works in ServiceNow


for (var i=0; i < payloadParsed.length; i++) { //Loop through the payload array


        //Do stuff with each result


        responseMessages[i] = "I did the thing to that line"; //Add some verbosity about that line. Did it work? Did it fail?


}


response.some_response_parameter = responseMessages; //Objects will automatically be 'JSON.stringify()'-ed, turning a Javascript object into a JSON payload string



METHOD 2 - Processor Script


Along the same line, you can make your own Processor. This is basically an endpoint, not strictly JSON or XML, that's callable like a UI page. E.g. instanceABC.service-now.com/my_processor.do



Note that this is really advanced, but gives you the most control. Again, I'd make it read through the payload that gets passed, and do stuff to the rows.



METHOD 3 - Email & Scripted Inbound Email Rule


Using some sneaky script, you can have an inbound email that meets specific requirements check for a CSV attachment, and load that as a Data Source, then run the import & transform. I haven't tried this myself, but here's some to get you started:


Data imports from email attachments



A big downside to this one is that there's no verbosity or response that gets sent back to the initiator like an API call would.



I wouldn't recommend one giant 'payload' field on your import staging table and having the transform read that, I tried it and ran into all sorts of limitations like JSON and XML translations stripping out essential characters, and errors that don't look like errors so your script doesn't know that something's gone wrong.




Hope this helps!