nil() vs isNil() - What's the difference?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2009 07:09 AM
What is the difference between the methods nil() and isNil()? I've seen both used in wiki and forum examples.
Example: Reference Field , http://community.service-now.com/node/1000544
This is where API docs would come in really handy.
- 58,025 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2009 07:36 AM
Looking at the source code, isNil() simply calls nil() and returns its value, so they appear to be identical.
There may be some history there, like maybe the Java version used to be isNil() and was refactored to nil(), but the method available to JavaScript could not be similarly refactored (because it was in use by scripts outside our control), so a JavaScript-available nil() was created for consistency and the isNil() changed to call it.
Or none of that may be true except for the first sentence of this post.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2015 12:20 PM
I found this in my email archive from yeas ago.
I thought it might be useful.
INT1427582: null vs isNil()
User instance: SNC instance - hfhs
2010-02-18 12:36:04 - Don Goodliffe | |
As it turns out..... I don't a null will ever be returned with the current code. At one time if you dot walked past a valid reference then you will be null back. For example if you are working on a task record and you do something like current.parent.parent.parent.parent and at some time there was no parent value YOU would get back a null. That is no longer the case and thus a statement like current.parent.parent.parent.isNil() will be true if at any point there is no parent value. However given that you have a current record then current.parent will NEVER be null. That is because current.parent does not return the value but returns the GlideElement for the parent field within the current GlideRecord object.... The fact that you are getting back a GlideElement object allows you then do do things like current.parent.isNil() to check to see if the value of the field is empty (null or the empty string) current.parent.setDisplayValue("INC10001") to set the parent field to point at incident INC10001 current.field_name has to return an object if you are going to invoke methods or continue dot walking in the case where feild_name is a reference field. So in my testing....with the current code.... current.cat_item.workflow will NEVER RETURN NULL even if cat_item itself does not have a value. | |
2010-02-18 08:39:54 - Paul Neuville | |
so !current.cat_item.isNil () is nearly the same as !current.cat_item.workflow == null since one executes a method call while the other checks a reference variable as valid. Since current.cat_item.isNil () is a method call, would it be safer to code it the way it was originally, just in case the reference variable current is invalid? I guess I am haveing difficulty understanding what isNil() is checking in either the data structure current.cat_item or current.cat_item.workflow as being nil. | |
2010-02-18 08:06:44 - James Capaldo | |
A clarification from a wiser colleague that may help: current.cat_item.workflow will return null if current.cat_item isNil(). In other words, attempting to dotwalk past a null item will return null. So a better and more easily understood check for our out-of-box rule would be the following: if (!current.cat_item.isNil() && !current.cat_item.workfow.isNil()) | |
2010-02-18 07:54:19 - James Capaldo | |
Hi Paul. isNil() checks for both a null value and a String length of zero and returns true in either case. In the business rule below, we are not first checking whether the current.cat_item.workflow field has a VALUE of null, but rather that current.cat_item.workflow is itself not null. If it were, doing a .nil() on it would throw a NullPointerException. Once we've confirmed that it is not null, then we check whether it's value is null or empty with the .nil() | |
2010-02-18 07:37:36 - Paul Neuville | |
This was found in global business rule sc_req_item_stageGetChoices | |
2010-02-18 07:33:01 - Paul Neuville | |
I know that nil() and isnil() are the same, but I've seen different ways to check for null value in the code that came with service now. what is the difference between these two: if (!me.rfc.isNil()) or if (encdata == null) And then there is this example where service now checks both? if (current.cat_item.workflow != null && !current.cat_item.workflow.nil()) this code taken from a service now business rule suggests there is a difference between the two. Can you explain this? |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2013 02:32 PM
And here we are 3+ years later, and this forum post is still the best piece of API documentation about isNil()...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2015 04:22 AM
Two more years have passed, and the API has been published on the recently launched ServiceNow Developer site:
https://developer.servicenow.com/app.do#!/api_doc
Strangely enough, it only mentions nil() whereas isNil() seems to have been omitted.
The same is true of the following Wiki article:
GlideElement - ServiceNow Wiki
Nonetheless, both methods can still be found in out-of-box code.
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/