Create Multiple records using JSON Payload
- 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2023 08:14 PM
Hi, without clear details of your payload your issue is difficult to evaluate.
What are the results of your debugging? What happens if you add debugging\logging inside your for loop IE
gs.info(parsedData.alerts[i].annotations.summary);
Are there mandatory requirements that are not being met as part of your 'em_event' record insert?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2023 09:31 PM
Hi This worked after I change my script as below
var reqbody = request.body.dataString; var parser = new global.JSON(); var parsedData = JSON.parse(reqbody); gs.info("Grafana : REQ " + reqbody); for(var i=0; i<parsedData.alerts.length; i++){ gs.info(parsedData.alerts[i].annotations.summary); var gr = new GlideRecord("em_event"); gr.initialize(); gr.description = parsedData.alerts[i].annotations.summary; gr.node = parsedData.alerts[i].labels.Customer;
gr.insert();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2023 08:18 PM
are you sure the for loop is running?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2023 10:22 PM - edited 07-19-2023 10:42 PM
Hi @Vamshi_ch123 ,
Need to end the for loop and check the length and values of the payload using logs
var reqbody = request.body.data;
var parsedData = reqbody.alerts;
gs.info("event : REQ " + reqbody);
gs.info("length is "+parsedData.alerts.length);
for(var i=0; i<parsedData.alerts.length; i++){
var gr = new GlideRecord("em_event");
gr.initialize();
gr.description = parsedData.alerts[i].annotations.summary;
gr.node = parsedData.alerts[i].labels.Customer;
gr.insert();
gs.info("testevent : val " + parsedData.alerts[i].valueString);
}
ServiceNow Community MVP 2024.
Thanks,
Pavankumar