which is best method to check null / not null

Supriya25
Tera Guru

Hi All,

 

Kindly help me on below issue

which is best method to check null / not null Performance wise/ Best Script  practice wise.

 

check  assigned_to Empty/Null/: 

if(gr.assigned_to==" "  && gr.sys_created_by !=gr.opened_by)

if(gr.assigned_to.nil()   && && gr.sys_created_by !=gr.opened_by)

if(gs.nil(gr.assigned_to)    && gr.sys_created_by !=gr.opened_by)

if(JSUtil.nil(gr.assigned_to)   && gr.sys_created_by !=gr.opened_by)

 

 

check assigned_to Not Empty/Not Null/:

if(gr.assigned_to!=" "  && gr.sys_created_by !=gr.opened_by)

if(!gr.assigned_to.nil()  && gr.sys_created_by !=gr.opened_by)

if(!gs.nil(gr.assigned_to)   && gr.sys_created_by !=gr.opened_by)

if(JSUtil.notNil(gr.assigned_to)   && gr.sys_created_by !=gr.opened_by)

 

Kindly help me on this, it has to be work for long-lasting .

 

 

 

 

1 ACCEPTED SOLUTION

Ravi Chandra_K
Kilo Patron
Kilo Patron

Hello @Supriya25 

Greetings!

  • Use strict equality (===) or strict inequality (!==) where possible, as it not only compares the values but also checks the data types. As this can prevent unexpected results due to type.
  • JSUtil is not available for scoped apps
  • Consider readability and maintainability of the code. It's essential to write code that is easy to understand and modify in the future.
  • I beleive gs.nil() and gs.notNil() are best practice in ServiceNow, as they handle null, empty strings, and undefined variables correctly.

refer below article:

https://www.servicenow.com/community/developer-articles/javascript-tips-and-tricks-gs-nill-glideelem...

Please hit the thumb and Mark as correct based on Impact!!

 

Kind Regards,

Ravi Chandra.

View solution in original post

6 REPLIES 6

Prince Arora
Tera Sage
Tera Sage

@Supriya25 

 

Best way is you can include into your addQuery/addEncodedQuery as follows:

gr.addEncodedQuery("assigned_toISNOTEMPTY");

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

Hi,

 

We always can't use addQuery/addEncodedQuery. Some times there would be chance to check in IF-statement

then which method would be better to follow.