The CreatorCon Call for Content is officially open! Get started here.

getValue on Integer, true false, and other non String in Scopped applications

corbettbrasing1
Mega Guru

I am on a quest for consistency in my code.   First off I work exclusively in scoped apps so I cannot use things like JSUtils as they are not available in a scoped application.   You also cannot use .nil() in scopped applications either.

Here is my goal:   I want to find a consistent way of getting values while avoiding pointer conflicts especially in loops but keep the proper variable type.

I am all about setters and getters but my problem is that GlideRecord.getValue() ALWAYS returns a string.   So if I want to get the value of true/false field and then use that value in an expression for example I cannot

I want to be able to use the "falsey" and "truthy" principles of javascript.   See this if you don't know what I am talking about Truthy and Falsy Values in JavaScript - A Drip of JavaScript

EMPTY Field examples

if(GlideRecord.getValue("fieldName")){//this will ALWAYS be true since get Value returns a string, even if its empty it returns a string of "null"

          //do something

}

But in order for that to actually work I need to do

if(GlideRecord.fieldName){//this does work as it returns an Integer of 0 which is false .

        //do something

}

TRUE FALSE Fields

var bool1 = GlideRecordObject.trueFalseField; //this will return a BOOLEAN value

var bool2 = GlideRecordObject.getValue("trueFalseField");//this will return a STRING, which means I cannot do Boolean("0") as this will evaluate to true.

var bool3 = GlideRecordObject.getDisplayValue("trueFalseField"); // returns a STRING of "true" or "false" which means I also cannot use Boolean("false")

//as any String is considered truthy and will return true

Dot walking though has the potential for pointer conflicts especially in loops I know for reference fields but I am not sure if they do for non reference fields.

So....what is the answer?   As far as I can tell I only have 3 options

1.   Use exact comparison with == against another string and always use getValue() which means I cannot use "truthy" and "falsey" in my logic

2.   Dot walk so I can use "truthy" and "falsey" and risk pointer conflicts.

3.   Create my own library that extends GlideRecord so I have all the functions and then make my own version of get value that return the proper type as far as "falsey" and "truthy" go.

Thoughts?   I don't like the idea of a mixed bag of dot walking and getters and setters.

8 REPLIES 8

Bernard A_ Mora
Tera Contributor

I use this when I need to retrieved a boolean value from the server side:

var userStatus = /true/.test(String(grUser.active));

 

String(grUser.active) returns "true" or "false", then you can use the test method on RegExp.prototype to convert the string to boolean.

Joey Day
Giga Guru

I wish I could go back in time and give you this six years ago. My friend and I just wrote a Script Include we call "BetterGetter". It is a better implementation of `getValue()` that returns proper native JavaScript types for GlideRecord values. You can get it from the Share site: Share › BetterGetter. Hope it's helpful, even so many years after you first asked this question. Cheers!

Brian O_Donnell
Tera Guru

People are still searching on this (including me...)
Found this from https://github.com/iamkalai/SNTIL/issues/66 (Thanks to n21lv !)

If you want to get a proper boolean from a GlideRecord, use: 

global.JSUtil.getBooleanValue(glide_record, field_name)

 

I discourage from doing this. Please see my response above. TL;DR: use !!GlideRecord.TrueFalseField.
The trick here is that JavaScript's valueOf (GlideElement.prototype.valueOf) is taking care of doing the actual conversation.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf