Create Multiple records using JSON Payload
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2023 07:39 PM
Trying to create multiple records from JSON using the below script but the records are not inserted.
Script:
var reqbody = request.body.data; var parsedData = reqbody.alerts; gs.info("event : REQ " + reqbody); for(var i=0; i<parsedData.alerts.length; i++){ var gr = new GlideRecord("em_event"); gr.initialize(); gr.setValue("description",parsedData.alerts[i].annotations.summary); gr.setValue("node",parsedData.alerts[i].labels.Customer); gr.insert(); gs.info("testevent : val " + parsedData.alerts[i].valueString);
-Thanks
5 REPLIES 5

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2023 11:18 PM
Hi,
It looks like there is an issue with the loop and the way you are accessing the parsedData variable. Based on the code you provided, it seems you are trying to insert multiple records into the em_event table based on the JSON data contained in the reqbody.alerts array.
Let's make some modifications to the script to fix the issues:
var reqbody = JSON.parse(request.body.data); // Parse the JSON data to a JavaScript object
var parsedData = reqbody.alerts;
gs.info("event : REQ " + JSON.stringify(reqbody));
for (var i = 0; i < parsedData.length; i++) {
var gr = new GlideRecord("em_event");
gr.initialize();
gr.setValue("description", parsedData[i].annotations.summary);
gr.setValue("node", parsedData[i].labels.Customer);
gr.insert();
gs.info("testevent : val " + parsedData[i].valueString);
}
Thanks,
Rahul Kumar
If my response helped please mark it correct and close the thread.
Thanks,
Rahul Kumar
Thanks,
Rahul Kumar