Bulk Upload in Staging Table Though Scripted RestApi

yadi
Tera Contributor

Hi Folks,

I have requirement in which Bulk Upload is require through External source in CMDB , I have created a Import Set Table and now i need whenever External source hit the ServiceNow API the whole data is dumping in staging table and after that transform maps are triggered. Earlier Row by Row is coming from external source and through Import Set Api it was working fine but now if Bulk Data is coming  the Import Set APi could not accept the data except 1st row as OOTB functionality.

I know this can be achieve by Scripted RestApi But i don't how to write exact logic so that whole bunch of data is dumping in Staging Table.

the data is coming like below JSON Format in one Array.

[{
"u_displayname": "abc123",
"u_vendor": "Cisco",
"u_primaryipv4address": "10.1.1.2",
"u_primaryosversion": "1.2.3.5",
"u_primaryostype": "Cisco IOS",
"u_osdescription": "Cisco IOS Software",
"u_physserialnumber": "abc123xyz",
"u_primarydnsname": "abc123.abc.com"
},
{
"u_displayname": "abc123",
"u_vendor": "Microsoft",
"u_primaryipv4address": "10.1.1.3",
"u_primaryosversion": "1.2.3.6",
"u_primaryostype": "Cisco IOS",
"u_osdescription": "Cisco IOS Software",
"u_physserialnumber": "abc123xyz",
"u_primarydnsname": "abc123.abc.com"
},
{

"u_displayname": "abc123",
"u_vendor": "HP",
"u_primaryipv4address": "10.1.1.4",
"u_primaryosversion": "1.2.3.7",
"u_primaryostype": "Cisco IOS",
"u_osdescription": "Cisco IOS Software",
"u_physserialnumber": "abc123xyz",
"u_primarydnsname": "abc123.abc.com"
}
]

If any one has solution of this requirement it would be great help

Regards

Yad

 

1 ACCEPTED SOLUTION

Hi,

create new scripted rest api and new scripted rest resource

Rest Resource -> POST Method

Script below:

1) iterate over the body

2) parse the json

3) for every object from the array

Sample below: Ensure you are receiving the above json request; I have assumed the json key is the column name

Also use proper import set table in GlideRecord

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
	
	var requestBody = request.body.dataString;
	var parser = new global.JSON();
	var parsedData = parser.decode(requestBody);
	
	for(var i=0;i<parsedData.length;i++){
		
		
		var gr = new GlideRecord('cmdb_test');
		gr.initialize();
		gr.u_displayname = parsedData[i].u_displayname;
		gr.u_vendor = parsedData[i].u_vendor;
		gr.u_primaryipv4address = parsedData[i].u_primaryipv4address;
		gr.u_primaryosversion = parsedData[i].u_primaryosversion;
		gr.u_primaryostype = parsedData[i].u_primaryostype;
		gr.u_osdescription = parsedData[i].u_osdescription;
		gr.u_physserialnumber = parsedData[i].u_physserialnumber;
		gr.u_primarydnsname = parsedData[i].u_primarydnsname;
		gr.insert();	
	}	
	var responseBody = {};
	responseBody.status = "Success";
	response.setBody(responseBody);
	
	
})(request, response);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

If you use scripted rest API you will have to inform the 3rd party team about the change in endpoint;

in scripted rest API do this

1) accept the request body

2) parse the json body

3) for every json object in the array create record in import set using GlideRecord

It would trigger the transform map

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

 

Thanks for joining, yup i now that that need to  inform 3rd party to change the endpoint but as i am new Scripted RestAPi so i need help on how to bulid logic in script section.

Suppose my Staging Table is CMDB_TEST now what logic i need to write in script section.Please help.

 

Regards

Yad

Hi,

create new scripted rest api and new scripted rest resource

Rest Resource -> POST Method

Script below:

1) iterate over the body

2) parse the json

3) for every object from the array

Sample below: Ensure you are receiving the above json request; I have assumed the json key is the column name

Also use proper import set table in GlideRecord

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
	
	var requestBody = request.body.dataString;
	var parser = new global.JSON();
	var parsedData = parser.decode(requestBody);
	
	for(var i=0;i<parsedData.length;i++){
		
		
		var gr = new GlideRecord('cmdb_test');
		gr.initialize();
		gr.u_displayname = parsedData[i].u_displayname;
		gr.u_vendor = parsedData[i].u_vendor;
		gr.u_primaryipv4address = parsedData[i].u_primaryipv4address;
		gr.u_primaryosversion = parsedData[i].u_primaryosversion;
		gr.u_primaryostype = parsedData[i].u_primaryostype;
		gr.u_osdescription = parsedData[i].u_osdescription;
		gr.u_physserialnumber = parsedData[i].u_physserialnumber;
		gr.u_primarydnsname = parsedData[i].u_primarydnsname;
		gr.insert();	
	}	
	var responseBody = {};
	responseBody.status = "Success";
	response.setBody(responseBody);
	
	
})(request, response);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks Ankur let me try this logic and get back to you.