Restrict user to select date

Evan2
Kilo Guru

Hi All,

How to restrict a user to select a date for a catalog variable which is less than 3 days from today and does not falls on weekends. 

I tried to do that by creating a date type field (o_expected_delivery_date) and  catalog client script and script include but this is not working. Please help me. PFB the scripts I have tried and help me to rectify this.

 

onChange Catalog Client script

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var da = new GlideAjax('ClientDateTimeUtils');
da.addParam('sysparm_name','getDefaultEndDate');
da.getXMLWait();


var end_date = da.getAnswer();
alert ('abd'+ end_date);

if(g_form.getValue('o_expected_delivery_date') < end_date)
{
alert('Date should be After 3 days from current date time');
g_form.clearValue('o_expected_delivery_date');
return;

}

}

 

Script Include 

ar ClientDateTimeUtils = Class.create();

ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDefaultEndDate: function() {
var startDate = gs.nowDateTime();
var addQuantity = this.getParameter('sysparm_quant');
var end_date = new GlideDateTime();
end_date.setDisplayValue(startDate);
end_date.addSeconds(259200);// Equivalent to 3 days
end_date = end_date.getDisplayValue();
return end_date;
},
});

 

onLoad client script 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var da = new GlideAjax('ClientDateTimeUtils');
da.addParam('sysparm_name','getDefaultEndDate');
da.getXMLWait();


var end_date = da.getAnswer();
alert ('abd'+ end_date);

if(g_form.getValue('o_expected_delivery_date') < end_date)
{
alert('Date should be After 3 days from current date time');
g_form.clearValue('o_expected_delivery_date');
return;

}

}

 

Regards,

Evan

 

 
14 REPLIES 14

Sohail Khilji
Kilo Patron
Kilo Patron

Hy,

 

Dont waste time writing script, do it with UI policy !

 

Refer my article on how to do it:

https://community.servicenow.com/community?id=community_article&sys_id=b9d340a41b3c0154c16b43f6fe4bc...

 

I hope the answer helps, You can thank me by Marking the answer  Correct or ???? Helpful.

Thanks,

MF Sohail Khilji.

LinkedIn > https://www.linkedin.com/in/mf-sohail-khilji/

 

 

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can check like this

1) first check if the selected date is weekend and if yes then return invalid

2) if it is not weekend then find the difference of today and selected date

Regards
Ankur

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

Hi,

updated script:

Script Include:

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getDefaultEndDate: function() {

		var selectedDate = this.getParameter('sysparm_date');
		var end_date = new GlideDateTime(selectedDate);
		var day = end_date.getDayOfWeekLocalTime();
		if(day == 6 || day == 7){
			return 'cannot select weekend';
		}
		else{
			var nowTime = new GlideDateTime();
			nowTime.addDaysUTC(3);
			if(end_date.getNumericValue() < nowTime.getNumericValue()){
				return 'Date should be After 3 days from current date time';
			}
			return '';
		}
	},

	type: 'ClientDateTimeUtils'
});

Client Script: onChange of o_expected_delivery_date

UI Type - ALL

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}

	if(oldValue != newValue){
		var da = new GlideAjax('ClientDateTimeUtils');
		da.addParam('sysparm_name','getDefaultEndDate');
		da.addParam('sysparm_date', newValue);
		da.getXMLAnswer(function(answer){
			if(answer != '')
			{
				alert(answer);
				g_form.clearValue('o_expected_delivery_date');
			}
		});
	}

}

Regards
Ankur

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

Thank You so much Ankur .. It worked 🙂