There are different data types in ServiceNow, when using JavaScript.
JavaScript is a “weakly” typed scripting language. Which means that when you declare a variable, you don’t have to define the data type of the variable when you create it, which is not how many other programming languages work.
Because we don’t have to define the data type when we declare the variable, that leaves it up to the JavaScript engine to decide what the data type of the variable is.
You have the following data types:
- Object
- String
- Boolean
- Number
- Null
- Undefined
That means that anything that you’re working with on the scripting side, can fall into any of the above 6 categories of a data type.
It’s usually beneficial to convert certain data types to a string, when working with the data.
This works best when you’re comparing two different variables for example.
This can be done by converting an object to a string, using a built in method, that is incredibly simple.
You can use any of the below scripts in a background script, to test it out. We will simply be declaring variables, with different values, and will be printing the data type of that variable.
The Incident Number Is An Object, By Default
When you loop through a simple GlideRecord query, for example – and you define a field like number, it will be of an object data type, which to me is confusing. The field type of this field is a “string” in ServiceNow, so why would the variable by an object?
To get around this, we simply convert the variable to a string, using object.toString()
The below script does NOT have the variable using the .toString() method converted.
var gr = new GlideRecord("incident");
gr.setLimit(1);
gr.query();
while (gr.next()) {
var number = gr.number;
gs.print(typeof number);
}
When you run this background script, it will print out the data type.
How To Get The Data Type Of A Variable
There is a built in JavaScript method to get the type of absolutely anything in a script.
You simply use the following structure:
typeof data_structure;
When you print this, you’ll get a data type printed out.
It couldn’t be easier to use.
Get TypeOf With ServiceNow’s JSUtil Script Include
You can use JSUtil, which is super helpful and is a ServiceNow Utility Script.
Go checkout this script include if you want to see a ton of other great utilities that you can use while scripting.
Consider the following example, where we have a data type of a Number, and we can print that out with JSUtil.type_of(a).
var a = 25;
gs.print("JSUtil.type_of(a) = " + JSUtil.type_of(a));
This will print out a data type of Number.
Feel free to change the variable and see what ServiceNow prints. You can easily do this in a background script, and not modify any data.
How To Convert A Variable To A String
This is my preferred method of converting a variable to a string.
You’re going to see this method all over the ServiceNow community and even in ServiceNow’s out of box scripts, so you know it’s supported.
var gr = new GlideRecord("incident");
gr.setLimit(1);
gr.query();
while (gr.next()) {
// use .toString() below
var number = gr.number.toString();
gs.print(typeof number);
}
When you print this, you’ll see that we’ve converted the object to a string.
Another Way To Convert A Variable To A String
This is a lesser known or even used in ServiceNow, but here’s another way to do the exact same thing.
We felt it was important to showcase this way as well, although you’re not going to see it done this way too frequently.
It will convert the gr.number object, to a string.
var gr = new GlideRecord("incident");
gr.setLimit(1);
gr.query();
while (gr.next()) {
// new String()
var number = new String(gr.number);
gs.print(typeof number);
}
Feel free to run any of the above examples and see what you get.
It’s completely safe to run these in a background script, as you’re not modifying any data. But it’s definitely recommended that you do this in sub production.
All of these are super helpful when you’re scripting, and specifically when you’re comparing different data types in conditional statements.
Let us know below if you have any tricks or tips here when scripting around data types in ServiceNow.