Best practice to check for booleans?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2017 05:27 AM
I picked up the habit of checking for <variable> == 'true', when checking for variables. Though this got me into trouble once, because someone entered the value 'True', resulting in a false negative. Should I check for <variable> == true instead? Or what is the best practice in ServiceNow for checking for boolean values?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2017 05:33 AM
JSUtil.toBoolean(value) will evaluate the value for a few different conditions. You can take a look at the JSUtil script include in your instance to see exactly what the code does.
However, you should be using a check box, or choice box for boolean values, so that is not dependent on the user typing in a value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2017 06:27 AM
The values come in through the web service and there we cannot use anything other than strings
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-02-2017 05:34 AM
I believe this should fall into java script best practices
For boolean values you can directly validate the variable in if condition
var check = true;
if(check){
// execute true conditions
}
if(!check){
// execute false conditions
}
Thanks
PS: Hit like, Helpful or Correct depending on the impact of the response
