Using Insert Multiple records via rest APi

Community Alums
Not applicable
var acc_cmdb_map = Class.create();
acc_cmdb_map.prototype = {
    initialize: function() {
        var fields_ar = "name,company,short_description,used_for,operational_status,ip_address,os,assignment_group".split(",");
        this.fields = fields_ar;
        this.cmdb_tables = {
            "cmdb_ci_aix_server": "u_imp_aix_server",
            "cmdb_ci_esx_server": "u_imp_esx_server",
            "cmdb_ci_linux_server": "u_imp_linux_server",
            "cmdb_ci_win_server": "u_imp_windows_server",
            "cmdb_ci_mainframe_lpar": "u_imp_server",
            "cmdb_ci_db_instance": "u_imp_database_instance",
            "cmdb_ci_appl": "u_imp_application_server"
        };
    },

    build_object: function(class_names) {
        this.tables_asked = class_names;
        var tbl = class_names.split(",");
        for (var i = 0; i < tbl.length; i++) {
            //		gs.info(this.cmdb_tables.hasOwnProperty(tbl[i]));
            if (this.cmdb_tables.hasOwnProperty(tbl[i])) {
                //   for (tbl[i] in this.cmdb_tables) {
                //	gs.info(tbl[i]+"  " +this.cmdb_tables[tbl[i]]);
                this.build_data(tbl[i], this.cmdb_tables[tbl[i]]);
                //   }
            }
        }
    },

    build_data: function(ttbl, import_set_tbl) {
        //gs.info("fields len : " +this.fields.length);
        var dta = new GlideRecord(ttbl),
            obj_arry = [],
            eqry = "nameISNOTEMPTY^operational_status=1^install_status=1";
        robj = {};
        dta.addEncodedQuery(eqry);
       dta.setLimit(10);
        dta.query();
        while (dta.next()) {
            var obj = {};
            for (var x = 0; x < this.fields.length; x++) {
                //		gs.info(x +" " +this.fields[x].toString() +" " +dta[x]);
                obj[this.fields[x].toString()] = dta[this.fields[x].toString()].getDisplayValue();
                /*              if (this.fields[x].toString() == "correlation_id") {
                                  obj[this.fields[x].toString()] = dta.sys_id;
                              } else {
                                  obj[this.fields[x].toString()] = dta[this.fields[x].toString()].getDisplayValue();
                              }*/
            }
            //      gs.info(JSON.stringify(obj));
            obj_arry.push(obj);
        }
       // gs.info(JSON.stringify("cmdb_data : " +obj_arry));
		var rr = {
		"records" : obj_arry
		};
		gs.info("rcds : " +JSON.stringify(rr));
//        this.push_data(JSON.stringify(obj_arry), import_set_tbl); //old working line
		
       this.push_data(JSON.stringify(rr), import_set_tbl); //new test
                robj = {};
                robj["records"] = [];
                robj["records"] = obj_arry;
                this.push_data(robj, import_set_tbl);
    },

    push_data: function(obj_arry, import_set_tbl) {
        gs.info(" Starting to Push data to Xtest for tables : " + this.tables_asked);
        gs.info("current : import_set table : " + import_set_tbl);
        gs.info("cmdb_data push : " +obj_arry);
        var request = new sn_ws.RESTMessageV2();
		request.setEndpoint('https://Xtest.service-now.com/api/now/import/' + import_set_tbl + '/insertMultiple');
        request.setHttpMethod('POST');
        request.setAuthenticationProfile('basic', 'f874b8eb1b2c19d05c1b11f3b24bcb97');
        request.setRequestHeader('Content-Type', 'application/json');
        request.setRequestBody(obj_arry);
    var response = request.execute();
		gs.log("Request Body Sent : \n Import set table : " +import_set_tbl +"\n Data Sent  :  "+request.getRequestBody());
        gs.log("Response Status : " +response.getStatusCode() + "Body : " + response.getBody());
    },

    type: 'acc_cmdb_map'
};

Hello, 

I have following script include to send multiple records in a single REST API and my log generates valid JSON but the target ServiceNow instance is inserting multiple records with just first record information. Please share your thoughts. 

 

Thank you. 

1 REPLY 1

Sumit Pandey1
Kilo Guru
Kilo Guru

Hi Manoj, Can you post a sample JSON that you're generating? For insertMultiple, your JSON should be formatted as defined here - Click here