How to import data from external application using API?

Jeff316
Kilo Guru

Hi All,

 

The day I have been dreading has arrived.  I have been given 12 API "paths" to an external application where I am to connect from ServiceNow and import their data into 12 custom tables I will create in ServiceNow. Can anyone point me to the document or post that describes how to connect to an external application using API and import the contents into custom table each night.  I don't need to authenticate. I'm going through the MID Server. I already took one of their API strings and used the ServiceNow API Explorer and could connect. I did a GET and saw a line of data returned. We are on London version.

1 ACCEPTED SOLUTION

Jeff316
Kilo Guru

Here is how I did it, in case anyone else needs help.

 

try {


//Send REST get to grab json blob
var r = new sn_ws.RESTMessageV2('noc.sd343.com Data Center List', 'get');
r.setStringParameter('Accept', 'application/json');
r.setStringParameter('key', 'Private');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

gs.log("Response Body " + responseBody);
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
gs.log("Parsed JSON" + parsed);
/*for (var n in parsed.value[0]){ //use this to see your json objects

gs.log("Parsed[" + n +"]= " + parsed.value[0][n]);

}*/

gs.log(parsed);

}catch(ex) {

var message = ex.getMessage();

}
//Create a new import set
var crImpSet = new GlideRecord('sys_import_set');
crImpSet.initialize();
crImpSet.mode = 'synchronous';
crImpSet.table_name = 'u_fnf_import_data_centers'; //Set the extended importset table
crImpSet.state = 'loading';
crImpSet.insert();
var locs = parsed.value;
gs.log("Parsed Array Size locs = " + locs.length);
var restGR = new GlideRecord('u_fnf_import_data_centers'); //Query extended import set rows table

//Loop through the json chunks until 0 remain creating import set rows to be transformed


for (var i = 0; i < locs.length; i++) {
restGR.initialize();
restGR.u_description = locs[i].description;
restGR.u_key = locs[i].key;
restGR.u_id = locs[i].id;
restGR.sys_import_set = crImpSet.sys_id; //This is the sys_id from import set above
restGR.insert();


}

View solution in original post

6 REPLIES 6

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi,

I would personally suggest you to go in deep with the following exercise in the Technology Partner Program, it will help you to understand how to do it easily (it's for jakarta and it should be the same for London as well):

Create a Web Service Import Set

If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.

Thank you

Cheers
Alberto

Alberto, 

Thanks.

Is this the method I use if there is a third party application called THIRD PARTY APP and they have an API exposed for "Data Center".

Goal: Each day I want to import the list of "Data Center" from THIRD PARTY APP into a custom table in my ServiceNow which I will call "Imported Data Centers".

 

Don't I need to create an Outbound REST message to GET from the THIRD PARTY APP and pull from their exposed API called "Data Center"?

 

Or do I use Inbound as describe in your link and use my custom table in ServiceNow called "Imported Data Centers" as the target? I don't see where in the related article I would reference a THIRD PARTY App to make the GET. I cannot use the API explorer to generate the scripts for the GET to a THIRD PARTY App.

 

Sorry, my bad, I understood wrongly your question.

Yes, you're right, you will have to write a scheduled script execution job which will make REST call to target instance ( GET ) and will bring the records what you need.

Once done, you can parse the response and put those records in your target instance.

Here you will find another way to do the same:

Re: Wanting to Import data using external REST service on a schedule using Import Set / Transform Ma...

Hope this will be useful.

Cheers

Alberto

Ok good. That is the post I was using but I'm terribly stuck writing the scheduled job script for the parsing. If I had one good example of a scheduled job script that queries an external API and returns 3 fields into the import set then to a target table.