why the query is taking first record even the variable doesn't get any value??

Manikantahere
Tera Contributor

var SerialNumber = "current.serial_number.toString() ";
var gr_device = new GlideRecord("alm_hardware");
gr_device.addQuery("serial_number", SerialNumber);
gr_device.query();
if (gr_device.next()) {

gs.print("asset found");
} else {

gs.print('asset not found');
}

 

when I got serial number and it is fetching corresponding record printing asset found as expected but there is a case because of field is empty serial number will not have any value in this case also I am getting asset found only.

why ? how to prevent such cases to not to go inside if ?

2 ACCEPTED SOLUTIONS

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

You can try to add gr.addNotNullQuery on serial number So your query becomes

var SerialNumber = "current.serial_number.toString() ";
var gr_device = new GlideRecord("alm_hardware");
gr_device.addQuery("serial_number", SerialNumber);
gr._device.addNotNullQuery("serial_number");
gr_device.query();
if (gr_device.next()) {

gs.print("asset found");
} else {

gs.print('asset not found');
}

 

-Anurag

View solution in original post

I'm guessing you have at least one record on this table with a blank serial number, so the GlideRecord is retrieving that record.

View solution in original post

5 REPLIES 5

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

You can try to add gr.addNotNullQuery on serial number So your query becomes

var SerialNumber = "current.serial_number.toString() ";
var gr_device = new GlideRecord("alm_hardware");
gr_device.addQuery("serial_number", SerialNumber);
gr._device.addNotNullQuery("serial_number");
gr_device.query();
if (gr_device.next()) {

gs.print("asset found");
} else {

gs.print('asset not found');
}

 

-Anurag

is that expected behavior? if I am getting nothing is it brings the first record? is that true ?

I'm guessing you have at least one record on this table with a blank serial number, so the GlideRecord is retrieving that record.

Ohh...so in this case it's trying to fetch empty serial number records as serial number consists nothing in it. Am I right?