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

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

Hi @Mani A - Thanks for the response, I have tried above code but I am getting error as length is not defined and it's undefined

I have modified it..but Error is because I have used different names instead of actual one "productSpecification"

 

Also in table insert records I have used gr.dataification  and it should be gr.specification_category= data.specification_categroy...you have use to use correct table name and backend name of fields.