The CreatorCon Call for Content is officially open! Get started here.

Set a default time in Date/Time calendar popup

Marcel H_
Tera Guru

I have a request from a team that wants to have the Due Date field on their custom table (extended from Task) have a default time set to 04:30:00PM whenever they open the date picker calendar popup for that field. I've read through quite a few posts here in the Community that provide solutions, many of which are shown marked as correct, but so far none of the methods have worked for me. So far I've tried the following:

onLoad Client Script on the custom table:

function onLoad() {
	var field = g_form.getControl('due_date');

if(field){
	Event.observe($(field.id).up(1).down('a'), 'click', function() {
	setTimeout(setDefaultTime,300); // Some time delay added
});     }

function setDefaultTime() {
$('GwtDateTimePicker_hh').value = '16';
$('GwtDateTimePicker_mm').value = '30';
$('GwtDateTimePicker_ss').value = '00';

}
}

onLoad Client Script on the custom table 2:

function onLoad() {

var field = g_form.getControl('due_date');
alert(field);
alert($(field.id).next('a'));

if(field){
Event.observe($(field.id).next('a'), 'click', function() {
setTimeout(setDefaultTime,300); // Some time delay added
}); }

function setDefaultTime() {
$('GwtDateTimePicker_hh').value = '16';
$('GwtDateTimePicker_mm').value = '30';
$('GwtDateTimePicker_ss').value = '00';

}
}

onLoad Client Script on the custom table 3:

function onLoad() {
   setDatePickerTime('due_date');
}

function setDatePickerTime(field){
   //Attach an event listener to set default time value for date picker
   Event.observe($(g_form.getControl(field).id).next('a'), 'click', function() {
   setTimeout("$('GwtDateTimePicker_hh').value = '16';$('GwtDateTimePicker_mm').value = '30';$('GwtDateTimePicker_ss').value = '00';",0);
   });
}

onLoad Client Script on the custom table 4, with Script Include call:

function onLoad() {
	
	var ajax = new GlideAjax('MyDateTimeAjax'); //Make call to script include
	ajax.addParam('sysparm_name', 'nowDateTime');
	ajax.getXML(function () {
		var test = ajax.getXMLAnswer(function(answer){
			var response = JSON.parse(answer);
			g_form.setValue('due_date', response.datetime); //Set value of Due Date with response from script include
		});
	});
}
var MyDateTimeAjax = Class.create();
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	nowDateTime: function () {
		var Date = new GlideDate(); 
		var datetime = (Date + ' 16:30:00');
		var response = {};
		response.datetime = datetime;
		return JSON.stringify(response);
	},
	type: 'MyDateTimeAjax'
});

 

So far none of these have worked, when I open the data picker, it still defaults to the current date/time, or if I use the Client Script/Script Include method is sets the field value onLoad, but I just want the date/time picker to have the default time set.

I also tried doing a Dictionary Override on for the Due Date field on the custom table and set the default value to javascript:gs.now() + " 16:30:00 ";  but that also doesn't change the default time displayed.

Any help on getting this to work as expected would be great. Thanks!

9 REPLIES 9

I got my issue , Thanks for your script 😉

Michaël

Hi,

were you able to make it work with the script shared by Sandeep or you have used another approach.

Regards

Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

mco
Tera Guru

I use the script of Sandeep as it was .

My issue was that I had to set as false the 'isolate scipt' field.

debasish_hm
Mega Contributor

Hi Sandeep and Michaël,

Thanks for this great script. Have you been able to make it work in Agent workspace. I have put together this script in my instance and it works as expected in the native UI, but I am not able to get it to work in Agent workspace. Any leads would be much appreciated.

Thanks,

Debasish

Hi Debasish

Same here, looking for exact same functionality in Workspace.

Did you found a solution?

Thanks
Marc