Determining if Something is an Array

I had an issue recently where I thought I was dealing with one kind of variable, but in fact was dealing with a different kind of variable.

During the course of debugging that particular block of code I realized that you can’t use typeof to determine whether or not something is an array.

Typeof returns ‘object’ for both an ‘object’:

const myObject = {

     “name”: “object 1”,

     “number”: 1

}

and an array:

let myArray = [1, 2, 3, 4]

As it turns out, there are at least two easy ways to determine whether or not something is an array.

Array.isArray() can be passed a variable and will return true if that variable is an array. Fortunately it is on old enough bit of functionality that it works with ServiceNow’s Rhino engine.

Additionally, ServiceNow’s gs.log() method will log out an array, but will log out ‘object’ for an object, so you can often tell what you’re dealing with just based on what you’re getting logged out as your script runs.

Populating a Duration Field

I recently had a use-case where I needed to populate a custom date/time field with time that had passed since a group of agile stories had been created.

Here is how I did that:

var currentTimeStamp = new GlideDateTime(); //Create current timestamp
currentTimeStamp = currentTimeStamp.getDisplayValue(); //Set it to a string so dateDiff can us it

var gr = new GlideRecord(‘rm_story’); //Get active, unassigned stories
gr.addQuery(‘active’, true);
gr.addQuery(‘sprint’, ”);
gr.addQuery(‘assigned_to’, ”);
gr.query();

while(gr.next()) { //Loop through the stories
     var startTimeStamp = gr.getDisplayValue(‘sys_created_on’);
     var dur = gs.dateDiff(startTimeStamp, currentTimeStamp, false);
     var durObject = new GlideDuration(dur);
     gr.u_time_since_creation = durObject; //Set value in custom field
     gr.update();
}

 

 

Throwing an Error Message From Inside of a Business Rule

I recently came across a situation where I needed to be able to log a user-visible message to the screen from a business rule.

Fortunately, this is possible:

var msg=’What you are trying to do is not allowed’;

gs.addErrorMessage(‘<script>var t=setTimeout(function(){var test = confirm(“‘ + msg + ‘”);if(!test){return false;}},500);</script>’ + msg);

Hat tip to Anurag Tripathi over on the community forums for the solution:

Logging Objects in ServiceNow

I believe that I’ve got a post from a ways back talking about how to log an object in ServiceNow. Since then, I’ve realized that there are two much easier ways to log out on object.

You can stringify the object before trying to log it, or you can use the JSUtils.logObject() method.

Here is a quick demo script:

var myObject = {
“name”: “Object1”,
“number”: 1
};

var stringObject = JSON.stringify(myObject);
gs.log(“My Test. myObject: ” + myObject);
gs.log(“My Test. stringObject: ” + stringObject);
JSUtil.logObject(myObject);

Here is the output:

 

 

 

 

Here is the official documentation for the JSUtil.logObject method:

https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_JSUtil-logObject_O_S