Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Running Countdown Timer on Incident form

Deepak Negi
Mega Sage
Mega Sage

Hi

I need to create a countdown timer on Incident Form which:

1. Runs continuously on the basis of current SLA's Time Left.

2. Pauses when the SLA is paused.

3. Refreshed when a new SLA is attahced to the Incident.

I have tried a timer using formatter and UI macro but I am not getting the exact logic to set the time for countdown.

Appeciate you help !!

Thanks

Deepak

5 REPLIES 5

sbanc
Kilo Guru

Hi Deepak

I'm having the exact same requirement. Did you ever find a solution for it?

Regards,

Simon

Shweta KHAJAPUR
Tera Guru

Hi Deepak

I also have the same requirement. Did you ever find a solution for it?

Regards,

Shweta K

Hi Shweta

I created a function for this. It's a bit inefficient, but it does what it should.

Here's how I got it to work:

  • Create a new field on the Incident form to display the countdown in afterwards
  • Create a Script Include with two functions to gather the information of the server side (SLA's and time left)
  • Create a Client Script which does the maths to create a countdown based on the next SLA target time and colors the field on the Incident form accordingly

Field attributes on Incident table:

find_real_file.png

Script Include:

var reusable_functions = Class.create();
reusable_functions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
           getSlaBreachDateTime: function() {
		try {
			// Parse for current Sysid of actual open object
			var currentSysid = this.getParameter('sysparm_current_sysid');
			var current = new GlideRecord('task');
			var taskSla = new GlideRecord('task_sla');
			var timeZone = gs.getSession().getTimeZoneName();
			var tz = Packages.java.util.TimeZone.getTimeZone(timeZone);
			var timeZoneOffSet;

			// get the current task object
			current.get(currentSysid);

			// Query for open SLA's and return the breach time if available
			taskSla.addEncodedQuery('task='+current.getUniqueValue()+'^stage=in_progress');
			taskSla.orderBy('time_left');
			taskSla.query();
			if(taskSla.next()) {
				var targetDateTime = new GlideDateTime(taskSla.planned_end_time);
				targetDateTime.setTZ(tz);
				timeZoneOffSet = targetDateTime.getTZOffset();
				targetDateTime.setNumericValue(targetDateTime.getNumericValue() + timeZoneOffSet);
				return targetDateTime; 
			} else {
				return;
			}

		} catch (e) {
			gs.error('Could not get SLA breach time - reusable_functions_-_getSlaBreachDateTime() - '+e);
		}
	},

	getPausedSlaBreachDateTime: function() {
		try {
			// Parse for current Sysid of actual open object
			var currentSysid = this.getParameter('sysparm_current_sysid');
			var current = new GlideRecord('task');
			var taskSla = new GlideRecord('task_sla');
			var timeZone = gs.getSession().getTimeZoneName();
			var tz = Packages.java.util.TimeZone.getTimeZone(timeZone);
			var timeZoneOffSet;

			// get the current task object
			current.get(currentSysid);

			// Query for open SLA's and return the breach time if available
			taskSla.addEncodedQuery('task='+current.getUniqueValue()+'^stage=paused');
			taskSla.orderBy('time_left');
			taskSla.query();
			if(taskSla.next()) {
				var targetDateTime = new GlideDateTime(taskSla.planned_end_time);
				targetDateTime.setTZ(tz);
				timeZoneOffSet = targetDateTime.getTZOffset();
				targetDateTime.setNumericValue(targetDateTime.getNumericValue() + timeZoneOffSet);
				return targetDateTime; 
			} else {
				return;
			}

		} catch (e) {
			gs.error('Could not get SLA breach time - reusable_functions_-_getSlaBreachDateTime() - '+e);
		}
	},
type: 'reusable_functions'
});

Client Script (onLoad):

function onLoad() {

	// Hide SLA time field for new records
	var state = g_form.getValue('state');

	if(g_form.isNewRecord() || state == 6 || state == 7 || state == 10 || state == 9 || state == 8) {
		g_form.setVisible('u_next_sla_time', false);
		return;
	}

	var gp = new GlideAjax('reusable_functions');
	gp.addParam('sysparm_name','getPausedSlaBreachDateTime');
	gp.addParam('sysparm_current_sysid', g_form.getUniqueValue());
	gp.getXMLWait();
	var pausedBreachDateTime = gp.getAnswer();

	if(pausedBreachDateTime != null){
		var answer = 'The SLA is currently paused!';
		g_form.setValue('u_next_sla_time',answer);
		gel('incident.u_next_sla_time').style.backgroundColor = "yellow";
		g_form.setReadOnly('u_next_sla_time', true);
		return;
	} else {

		var ga = new GlideAjax('reusable_functions');
		ga.addParam('sysparm_name','getSlaBreachDateTime');
		ga.addParam('sysparm_current_sysid', g_form.getUniqueValue());
		ga.getXMLWait();
		var breachDateTime = ga.getAnswer();

		// Hide SLA time field if there are no SLA's assigned to it
		if(breachDateTime == null) {
			g_form.setVisible('u_next_sla_time', false);
			return;
		}

		// Get the target Date Time object to count down to
		var targetDateTime = new Date(breachDateTime);
		var countDownDate = targetDateTime.getTime();
		createCountdown(countDownDate);
	}
}

function createCountdown(countDownDate) {
	var x = setInterval(function() {
		var now = new Date().getTime();

		// Find the distance between now an the count down date
		var distance = countDownDate - now;

		// Time calculations for days, hours, minutes and seconds
		var days = Math.floor(distance / (1000 * 60 * 60 * 24));
		var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
		var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
		var seconds = Math.floor((distance % (1000 * 60)) / 1000);
		var answer = '';

		// If the countdown is expired, display a SLA breached message
		if (distance < 0) {
			clearInterval(x);
			answer = 'The SLA has breached!';
			g_form.setValue('u_next_sla_time',answer);
			gel('incident.u_next_sla_time').style.backgroundColor = "red";
			g_form.setReadOnly('u_next_sla_time', true);
			return ;
		} else {
			answer = 'You have '+days+'d '+hours+'h '+minutes+'m '+seconds+'s left until next SLA breaches';
			g_form.setValue('u_next_sla_time',answer);
			gel('incident.u_next_sla_time').style.backgroundColor = "greenyellow";
			g_form.setReadOnly('u_next_sla_time', true);
			return ;
		}
	}, 1000);
}

It is all just a copy of mines, you definitely need to adjust it to what your needs and states ect. are. But I think it should help you at all to create yours.

Here's the final product looks like on the incident form:

find_real_file.png

find_real_file.png

find_real_file.png

 

Hope it will help you

Cheers,

Simon

This code worked like a Charm!  Thanks Simon