Null doesn't work while setting values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2023 09:46 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2023 10:05 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2024 04:16 AM
Why? It's said in the documentation that direct assignment is the same as setValue() call.
setValue(String name, Object 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2023 10:26 PM
@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 !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2024 06:32 AM
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.