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

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.

can we use       if( gr.assigned_to == " "  )     if(  gr.assigned_to  != " " )   ?

Hey Supriya!

Yes you use that.

also try gr.getValue(assigned_to) // getValue returns null when the value is empty string,but it also returns null when the value is bool "false". 

Try log statements to check values you are getting.

 

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

Kind Regards,

Ravi Chandra.

Hello @Supriya25 

Greetings!

Any feedback on my reply? were you able to do this?

Please mark the answer as correct solution and helpful if helped.

 

Kind Regards,

Ravi Chandra