Create Multiple records using JSON Payload

Vamshi_ch123
Tera Contributor

Hi @Pavankumar_1 

 

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

Tony Chatfield1
Kilo Patron

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?

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Vamshi_ch123 

are you sure the for loop is running?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Pavankumar_1
Mega Patron

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

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar