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

I have seen this and am a little confused, How can i ensure that the coalesce field is populated on insert?

I am sending a postman request like so 

{"u_serial_number":"B4FQQTR13","u_bios_password":"1312456741215e"}

 

 

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);