Confusion Over NULL

stevejarman
Giga Guru

I need a little help understanding how ServiceNow handles NULL values. I've just tracked a bug in our system back to some code where we'd implemented what we thought was the correct use of "NULL", according to this document:

Setting a GlideRecord Variable to Null - ServiceNow Wiki

Here's that code:

// RESET FORM

current.comments = "NULL";

current.work_notes = "NULL";

current.u_work_type = "NULL";

current.u_work_started_dt = "NULL";

current.u_work_finish_dt = "NULL";

This always seemed suspicious as comments and work_notes are text-based fields, and I could never understand how ServiceNow knows that I'm not simply trying to assign the string "NULL"? It turns out, ServiceNow doesn't know - the result of the above code being that comments and work_notes get set to "NULL" (the string) and the other fields get set to null (the concept).

I've just tried replacing this code with the following (i.e. the JavaScript null):

// RESET FORM

current.comments = null;

current.work_notes = null;

current.u_work_type = null;

current.u_work_started_dt = null;

current.u_work_finish_dt = null;

and this seems to work perfectly (i.e. sets all of the fields to what I understand to be null), despite the note in the above article which indicates that the JavaScript null isn't the correct way to go.

So, is the correct option to do the following?

// RESET FORM

current.comments = null;

current.work_notes = null;

current.u_work_type = "NULL";

current.u_work_started_dt = "NULL";

current.u_work_finish_dt = "NULL";

I'm hoping it's not, because that seems illogical.

Can anyone clarify this for me?

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Steve,



That wiki article is suspicious to me. When comparing, it's always.



if (current.getValue('fieldname') == null)



or better yet



if (!current.getValue('fieldname'))



Stick with null where string values are concerned. Putting something in quotes turns it in to a string. So "NULL" will save the string NULL in the comments. Not what you want.



As far as querying, there is a GlideRecord method called



addNullQuery() and addNotNullQuery() to search for fields with null values (or not null values respectively.)



Reference: GlideRecord - ServiceNow Wiki


View solution in original post

3 REPLIES 3

stevejarman
Giga Guru

As a side note, if I wanted to query a text field containing the string "NULL", how would I do that?



e.g. this code:



var rs = new GlideRecord("task_time_worked");


rs.addQuery("u_work_notes", "NULL");


rs.query();



Looks for the literal null.


Chuck Tomasi
Tera Patron

Hi Steve,



That wiki article is suspicious to me. When comparing, it's always.



if (current.getValue('fieldname') == null)



or better yet



if (!current.getValue('fieldname'))



Stick with null where string values are concerned. Putting something in quotes turns it in to a string. So "NULL" will save the string NULL in the comments. Not what you want.



As far as querying, there is a GlideRecord method called



addNullQuery() and addNotNullQuery() to search for fields with null values (or not null values respectively.)



Reference: GlideRecord - ServiceNow Wiki


Thanks for that - comparisons was going to be my next question...



if (current.getValue('fieldname') == "NULL")



was really going to upset me