- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 07:40 AM
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
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 07:56 AM - edited 09-13-2024 04:55 PM
can you try this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 08:00 AM - edited 09-13-2024 08:01 AM
@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();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 08:42 AM
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