
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
NOTE: MY POSTINGS REFLECT MY OWN VIEWS AND DO NOT NECESSARILY REPRESENT THE VIEWS OF MY EMPLOYER, ACCENTURE.
DIFFICULTY LEVEL: INTERMEDIATE
Assumes having taken the class SSNF and has good intermediate level of knowledge and/or familiarity with Scripting in ServiceNow.
Something I see as a common Scripting error is the use of adding a couple of integers and the values getting implicitly converted to a string. For example:
var integerOne = "15"; // string
var integerTwo = 30; // number
gs.info(integerOne + integerTwo);
Will produce:
*** Script: 1530
Nice, huh? Welcome to JavaScript. 😝
JavaScript is a loosely "typed" computer language (i.e. there is no way to explicitly specify a variable as an Integer, Decimal, and so on). The language has to make a judgement call on what to do with the values during run-time. If it runs into a situation like that demonstrated above, it takes the lowest common denominator (string) and converts everything to that. THEN since the plus ("+") symbol is overloaded in functionality to mean "Add" (if two numbers) or "Concatenate" (if two strings) the language happily concatenates the two values together.
This can lead you to times where you say things like:
"I HATE JAVASCRIPT!"
"JavaScript Stinks!"
"I Wish JavaScript Were a Real Language!"
So, let's explore this topic a little (implicit conversion, not the "I hate…" part).
JavaScript provides us with ways to force the language to view the variable values as they were intended. This can be tedious, but if you must have proper handling of values it is the only way.
The two ways I will present here are:
- parseInt
- parseFloat
parseInt
The first of these I want to describe is parseInt. This allows you to force a string integer value to be an actual integer value. In the following example integerValue is an actual integer:
var integerCheck = "15";
var integerValue = parseInt(integerCheck); // convert to actual integer
However you can mess this up by trying to then add it to a string integer. If you do that it converts it to a string and concatenates it to the other string value.
var integerCheck = "15";
var integerValue = parseInt(integerCheck); // convert to actual integer
integerValue = integerValue + integerCheck; // casts back to a string!
gs.info(integerValue); // 1515
Will generate these expected results:
*** Script: 1515
Doing a typeof will verify that it is string and help you be able to avoid this sort of problem:
gs.info(typeof integerValue); // string
Nice, huh? But before you get to cussing… this will fix it:
var integerCheck = "15";
var integerValue = parseInt(integerCheck); // convert to actual integer
integerValue = parseInt(integerValue) + parseInt(integerCheck);
gs.info(integerValue); // 30
Will give you the result of:
*** Script: 30
Much better!
Doing a typeof will verify that it is truly a number:
var integerCheck = "15";
var integerValue = 15; // re-cast it to an integer
integerValue = integerValue + parseInt(integerCheck); // works correctly
gs.info(integerValue); // 30
Expected result:
*** Script: 30
Here is another example of careful conversion:
integerValue = 15; // re-cast it to an integer
integerValue = integerValue + parseInt(integerCheck); // works correctly
gs.info(integerValue); // 30
Expected result:
*** Script: 30
So that covers integers pretty well. What about decimal numbers?
parseFloat
Starting out with a string representation of a decimal we can convert it to an actual decimal by doing a parseFloat. Example:
var decimalCheck = "20.5";
var decimalValue = parseFloat(decimalCheck); // convert to actual decimal
gs.info(decimalValue); // 20.5
gs.info(typeof decimalValue); // number
Results:
*** Script: 20.5
*** Script: number
We can see it works exactly the same as parseInt.
Now, notice what happens if we don't convert all of the values:
decimalValue = decimalValue + decimalCheck; // casts back to a string!
gs.info(decimalValue); // 20.520.5
gs.info(typeof decimalValue); // string
Results:
*** Script: 20.520.5
*** Script: string
Yup! Same crummy behavior. We get a useless string-i-fied value.
However with decimal strings; if you convert everything to to a decimal number you will notice an error creeping into the result. This is because of the way JavaScript handles the conversions under-the-hood. It does not do the greatest job with this sort of stuff. Because of this I tend to stay far away from parseFloat.
decimalValue = parseFloat(decimalValue) + parseFloat(decimalCheck); // now it works correctly
gs.info(decimalValue); // 41.019999999999996
Results:
*** Script: 41.019999999999996
parseBool
First, let me make it clear, there is no such thing in the JavaScript language as parseBool! That doesn't keep me from wishing I had the functionality from time-to-time though. So here you go. An example of the one I use.
function parseBool(value) {
return (/^(true|1|yes|on)$/i).test(value);
}
Some testing:
var boolCheck = 'false';
// various tests for parseBool
gs.info(parseBool('YES')===true); // true
gs.info(parseBool('no')===true); // false
gs.info(parseBool('on')===true); // true
gs.info(parseBool(boolCheck)===true); // false
gs.info(parseBool('true')===true); // true
gs.info(parseBool('false')===true); // false
function parseBool(value) {
return (/^(true|1|yes|on)$/i).test(value);
}
Results:
*** Script: true
*** Script: false
*** Script: true
*** Script: false
*** Script: true
*** Script: false
Anyway, there you go, now you know a bit about the JavaScript parseInt, and parseFloat and why you might need them while Scripting in ServiceNow.
Enjoy!
Steven Bell.
If you find this article helps you, don't forget to log in and mark it as "Helpful"!
Originally published on: 7-14-2016 07:55 AM
I updated the code and brought the article into alignment with my new formatting standard.
- 4,532 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.