The CreatorCon Call for Content is officially open! Get started here.

Create Multiple records from the payload using scripted REST API

Prudhvi Raj4
Tera Guru

Hello Team,

 

I am trying to create multiple created from payload coming from the 3rd party to servicenow, using scripted REST API I want read the payload and create records on target table and we want to call script include from the REST API as we may reuse the same code from in other API's

Sample Payload:

{
"productSpecification": [
{
"name":"NetApp on Equinix Metal Storage Commitment: Extreme",
"display_name":"NetApp on Equinix Metal Storage Commitment: Extreme",
"specification_category": "storage / Storage",
"start_date": "2023-09-01",
"u_billing_type":"",
"external_id":"1-006-605",
"u_billable": "false",
"description": "NetApp on Equinix Metal Storage Commitment: Extreme",
"code": "1-006-605",
"u_sales_uom": "",
"product_code": "",
"product_line": ""

},
{
"name":"NetApp on Equinix Metal Storage Commitment: Extreme",
"display_name":"NetApp on Equinix Metal Storage Commitment: Extreme",
"specification_category": "storage / Storage",
"start_date": "2023-09-01",
"u_billing_type":"",
"external_id":"1-006-606",
"u_billable": "false",
"description": "NetApp on Equinix Metal Storage Commitment: Extreme",
"code": "1-006-606",
"u_sales_uom": "",
"product_code": "",
"product_line": ""

},
]

 

Please suggest me how do I can create multiple records from above payload using scripted REST API and Script Include

 

Regards,

Prudhvi
}

1 ACCEPTED SOLUTION

Mani A
Tera Guru

can you try this

var payload = request.body.data;
for (var i = 0; i < payload.productSpecification.length; i++) {
    var data = payload.productSpecification[i];
    var result = new ScriptInclude().createRecords(data);
}


createRecords: function(data) {
        var gr = new GlideRecord('your_target_table');

       
        gr.initialize();
        gr.name = data.name;
        gr.display_name = data.display_name;
        gr.specification_category = data.specification_category;
        gr.start_date = new GlideDateTime(data.start_date);
        gr.u_billing_type = data.u_billing_type;
        gr.external_id = data.external_id;
        gr.u_billable = data.u_billable;
        gr.description = data.description;
        gr.code = data.code;
        gr.u_sales_uom = data.u_sales_uom;
        gr.product_code = data.product_code;
        gr.product_line = data.product_line;

        return gr.insert();
    }

View solution in original post

6 REPLIES 6

Yuvan Raj Kuma1
Tera Contributor

@Prudhvi Raj4  Hope this will help you

 

Step 1

API : go to scritped rest api and create your API 
Method : POST
Note : The below code will not do any validation please add it

 

//Get request body 
var requestBody = request.body.data;

//Creating Obj
   var scriptInc = new global.<your_script_include_name>();
   scriptInc .createRecord(requestBody);
    response.setStatus(200);
    response.setBody({
        message: 'All records inserted successfully'
    });

 

 

Step 2: Create Script Include

Create below function in your script include

Note : No validation is added pls add as per your requirement

 

createRecord: function(data) {

    var requestBody = data;
    var errors = [];

    for (var i in requestBody) {

        var record = requestBody[i];

        var tbl = new GlideRecord( < table_name > );
        invStgTbl.newRecord();
        invStgTbl.setValue("servicenow_field_Name", record. < your_api_key_name>);
        // Example :   invStgTbl.setValue("servicenow_field_Name", record.name)
        invStgTbl.insert();
    }

}

 

 

Hi @Yuvan Raj Kuma1 - Thanks for the response, can you please let me know what "invStgTbl" in the above script it is not declared anywhere