Returing undefined if the value is empty

akhila13
Tera Expert

 i am using a multi row variable set 'req' having two variables name and url.

 

When an incident ticket is submitted i want to insert the values in the table u_ur having fileds name1 and url1.

 

but when the url1 or name1 is empty value 'undefined' is inserted into the table'.

 

Anyone please help

 

 

var array = JSON.parse(producer.req);
//var obj=JSON.parse(a);
var grlink = new GlideRecord("u_ur");
for (var i = 0; i < array.length; i++) {
grlink.initialize();
grlink.setValue("u_num", current.getUniqueValue());
grlink.setValue("u_name1", array[i].name);
grlink.setValue("u_url1", array[i].url);
grlink.insert();

}

 

3 REPLIES 3

Sagar Pagar
Tera Patron

Hi @akhila13,

Try this updated scripts and let me know.

 

var array = JSON.parse(producer.req);
//var obj=JSON.parse(a);
var grlink = new GlideRecord("u_ur");
for (var i = 0; i < array.length; i++) {
grlink.initialize();
grlink.setValue("u_num", current.getUniqueValue());
if (array[i].name != '') {
grlink.setValue("u_name1", array[i].name);
}
if (array[i].url != '') {
grlink.setValue("u_url1", array[i].url);
}
grlink.insert();

}

 

Thanks,
Sagar Pagar

The world works with ServiceNow

Pavankumar_1
Mega Patron

Hi @akhila13 ,

can you try below script

var array =  producer.variables.req;
var grlink = new GlideRecord("u_ur");
for (var i = 0; i < array.length; i++) {
grlink.initialize();
grlink.setValue("u_num", current.getUniqueValue());
grlink.setValue("u_name1", array[i].name);
grlink.setValue("u_url1", array[i].url);
grlink.insert();
}
If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Pavankumar_1
Mega Patron

Hi @akhila13 ,

If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

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