Null doesn't work while setting values

Akki1
Tera Contributor

Hi,

In my scripted rest api I'm receiving some values which I'm using to set values in a table


var Exception_Name="";

var name="";

name=entry.name;

Exception_Name=entry.Exception_Name;

var gd=GlideRecord("u_custom_table");

gd.addQuery("u_name",name);

gd.query();

if(gd.next()){

gd.u_exception_name=Exception_Name;

gd.update();

}

 

Here if I get the value of entry.Exception_Name as null it is not updating it and keeping the old value but if there comes another value it updates it with it.It is if String type.
Can anyone tell me why this behaviour is happening?And how can I fix it

 

4 REPLIES 4

Maik Skoddow
Tera Patron
Tera Patron

Hi @Akki1 

never use direct assignments like

gd.u_exception_name=Exception_Name

 

This is wrong! Always use the setValue() method in the GlideRecord and instead of NULL use en empty String for clearing a field:

gd.setValue('u_exception_name', '');

 

Maik

Why? It's said in the documentation that direct assignment is the same as setValue() call.

setValue(String name, Object value)

Sets the value of the field with the specified name to the specified value.

Normally the script does a now_GR.category = value. However, if the element name is itself a variable then you can use now_GR.setValue(elementName, value). When setting a value, ensure the data type of the field matches the data type of the value you enter.



https://developer.servicenow.com/dev.do#!/reference/api/xanadu/server/no-namespace/c_GlideRecordScop...

SANDEEP28
Mega Sage

@Akki1 Use below script. 

 

var grCustomTable = GlideRecord('u_custom_table');
grCustomTable.addQuery('u_name', entry.name);
grCustomTable.setLimit(1);
grCustomTable.query();
if(grCustomTable.next()){
grCustomTable.setValue('u_exception_name', entry.Exception_Name);
grCustomTable.update();
}

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !! 

Runjay Patel
Giga Sage

Hi @Akki1 ,

 

Best way you can handle is to check both field with empty or undefined, if any of the value having empty or undefined the. Do not execute the glide record query as there is no use.

 

or if you want to update with empty value then check if exception name is undefined then set exception name=‘’ and then execute the code it will work. 

you can not update null or undefined value.