Scripted rest API: POST multiple records to import set table

DanielCordick
Mega Patron
Mega Patron

Im stuck with a Scripted Rest API. I am going to be posting JSON to ServiceNow via powershell. I would like to have one POST but with Multiple records like so 

 

[{"u_serial_number":"412415","u_bios_password":"abc"},{"Serial Number":"134124f","u_bios_password":"defgfhgshg"}]
 
When i use postman or the REST API Explorer i get a 200 status code but no records are imported or updated, here is my script 
 
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    
    var sn;
    var bios;
    var requestBody = request.body;
    var requestString = requestBody.dataString;
    var obj = JSON.stringify(requestString);
   // gs.log("The Object ", obj);

    for (i = 0; i < obj.length; i++) {
        sn = obj[i].u_serial_number; // Device Serial Number
        bios = obj[i].u_bios_password; //New PW to update

        var record = new GlideRecord('u_bios_import');
        record.initialize();
        record.u_serial_number = sn;
        record.u_bios_password = bios;
        record.insert();
    }



    return {
        "response": "Successfully saved request into " + request.pathParams.tablename + " table.",
        "requestString": requestString
    };

})(request, response);
 
1 ACCEPTED SOLUTION

DanielCordick
Mega Patron
Mega Patron

Anyone looking to loop through a Scripted Rest message to update an import set table, With the help of Mike and Pradeep, this is working for me, modify for your needs

 

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

var sn;
var bios;
var requestBody = request.body;
var requestString = requestBody.dataString;

var parser = new JSONParser();
var result = parser.parse(requestString);
//gs.log("******", result);

for (var i = 0; i <result.length; i++) {
sn = result[i].u_serial_number; // Device Serial Number
bios = result[i].u_bios_password; //New PW to update

var record = new GlideRecord('tablename');// Staging table name
record.initialize();
record.u_serial_number = sn;
record.u_bios_password = bios;
record.insert();

}


return {
"response": "Successfully saved request into " + request.pathParams.tablename + " table.",
"requestString": result
};

})(request, response);

 

 

 

 

View solution in original post

6 REPLIES 6

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Danny,

Could you please try with the updated code below.

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    
    var sn;
    var bios;
    var requestBody = request.body;
    var requestString = requestBody.dataString;
    var obj = JSON.stringify(requestString);
   // gs.log("The Object ", obj);

 for (var key in requestString)
        sn = requestString[key].u_serial_number; // Device Serial Number
        bios = requestString[i].u_bios_password; //New PW to update

        var record = new GlideRecord('u_bios_import');
        record.initialize();
        record.u_serial_number = sn;
        record.u_bios_password = bios;
        record.insert();
    }



    return {
        "response": "Successfully saved request into " + request.pathParams.tablename + " table.",
        "requestString": requestString
    };

})(request, response);

Note: Untested code.

 

- Pradeep Sharma

Mike Patel
Tera Sage

try

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
	var sn;
	var bios;
	var requestBody = request.body;
	var requestString = requestBody.dataString;

	var obj = JSON.stringify(requestString);

	var parser = new JSONParser();
	var parameterArr = parser.parse(obj);

	for (var i = 0; i < parameterArr.length; i++) {
		sn = parameterArr[i].u_serial_number; // Device Serial Number
		bios = parameterArr[i].u_bios_password; //New PW to update

		var record = new GlideRecord('u_bios_import');
		record.initialize();
		record.u_serial_number = sn;
		record.u_bios_password = bios;
		record.insert();
	}
	return {
		"response": "Successfully saved request into " + request.pathParams.tablename + " table.",
		"requestString": requestString
	};

})(request, response);

Hey Mike,

 

I believe it is working, Now im getting this error on my transform map

 

on my transform map i am going to coalesce on Serial_number on the CMDB_CI table.

 

"800 - Unable to resolve target record, coalesce values not present: u_serial_number"

 

 

Hi Danny,

 

Could you please try the resolution steps mentioned  here https://hi.service-now.com/kb_view.do?sysparm_article=KB0547762

 

-Pradeep Sharma