Get GlideDuration value in millseconds

Curiously, ServiceNow’s GlideDuration object gives you a way to set the duration in milliseconds (i.e. passing in the millisecond value when constructing a new instance) but there’s no simple way to get back that duration expressed in milliseconds. You can get it back expressed in days, hours, minutes, and seconds in just about any format you might need, but not in total milliseconds. I recently had a use case for expressing a duration in total hours, where being able to get back a millisecond value would have been very convenient. But instead I had to get creative with string manipulation to convert the duration display value back into milliseconds. If you need to do this too, do yourself a favor and put it in a Script Include so it’s reusable.

Here’s my Script Include, which you are welcome to copy verbatim or use as inspiration for your own (at the very least, I recommend replacing “ACME” with your company’s name or initials, or just name it according to whatever convention you would normally follow at your organization):

var ACMEDurationHelper = Class.create();
ACMEDurationHelper.prototype = {
    initialize: function() {
    },
	
	// Accepts a GlideDuration object, passes back total milliseconds of the duration.
	getTotalMS: function(dur) {
		var durVal = dur.getDurationValue();

		// Parse the duration value into days and time
		var durValArr = durVal.split(' ');
		var days = 0;
		if (durValArr.length == 2) {
			var days = parseInt(durValArr.shift(), 10);
		}
		var time = durValArr.shift();

		// Parse the time into hours, minutes, and seconds
		var timeArr = time.split(':');
		var hours = parseInt(timeArr[0], 10);
		var minutes = parseInt(timeArr[1], 10);
		var seconds = parseInt(timeArr[2], 10);

		// Calculate and return total milliseconds
		return days * 86400000 + hours * 3600000 + minutes * 60000 + seconds * 1000;
	},
	
	getTotalSecs: function(dur) {
		return this.getTotalMS(dur) / 1000;
	},
	
	getTotalMins: function(dur) {
		return this.getTotalMS(dur) / 60000;
	},
	
	getTotalHrs: function(dur) {
		return this.getTotalMS(dur) / 3600000;
	},

	getTotalDays: function(dur) {
		return this.getTotalMS(dur) / 86400000;
	},

	type: 'ACMEDurationHelper'
};

If this is useful to you, drop me a comment to let me know. Cheers! 

Metric for Updated By

Defining a ServiceNow Metric to track every update to any record in a table

A friend recently asked how he could create a ServiceNow Metric that would track who makes each and every update to records in a given table. Sounds easy enough, right?

Well, it turns out the only fields that would make sense to trigger such a metric are system fields like sys_updated_on or sys_updated_by, and the tricky thing is those fields don’t get monitored by default for calculating metrics. I searched the ServiceNow Community and found a way to do it, and below I am providing an off-the-shelf Metric Definition and Business Rule that will track every time a record is updated on a table.

The Metric Definition

I’ve defined this for the Task table (which works for any tables extended from Task), but you’re of course welcome to make it more specific as your needs require.

Name: Task Updated By
Table: Task [task]
Field: Updated by
Type: Script calculation

Script:

createMetric(current.sys_updated_by.toString());

function createMetric(value) {
	var mi = new MetricInstance(definition, current);
	var gr = mi.getNewRecord();
	gr.field_value = value;
	gr.calculation_complete = true;
	gr.insert();
}

The Business Rule

This is the secret sauce. Since fields like sys_updated_by aren’t monitored for metric calculations, we need to explicitly declare that we want it monitored by setting up a Business Rule to fire the same event that would normally be fired for non-system fields.

Name: Metric Event for Updated By
Table: Task [task]
Advanced: true
When: after
Insert: true
Update: true

Script:

function onAfter(current, previous) {
   gs.eventQueue('metric.update', current, '[sys_updated_by]', 1, 'metric_update');
}

That’s all there is to it. Now any time someone inserts or updates any Task (or records in whatever table you specified instead) you’ll get a new Metric Instance with a value containing that User’s user name. 

Integrating ServiceNow with HipChat

My tutorial on integrating ServiceNow with Slack turned out to be one of my most popular articles ever. Justin Meader recently asked me on Twitter how easy it would be to integrate Service­Now with HipChat instead.

So, I gave it a whirl. In this article I’ll show you how you can post Service­Now notifications right into HipChat using their API. Just like in the previous tutorial, I’ll give you a ready-made Service­Now Script Include and instructions and examples for how you can call that Script Include from any scripted Business Rule.

Continue reading

Integrating ServiceNow with Slack

Slack + ServiceNow One of our teams at work recently started using Slack for all their team communication and approached me about possibly having ServiceNow shoot Incident assignment notifications into one of their Slack channels. As it turns out, integrating ServiceNow and Slack is really easy. In this article I’ll show you how to enable the necessary Slack “Incoming Webhooks” integration service as well as give you a ready-to-go ServiceNow Script Include you can use to post messages to Slack from any scripted ServiceNow Business Rule. Finally, I include some examples and ideas for how to use and re-use the Script Include in your own Business Rules.

Continue reading