How to restrict saturday and sunday using UI Policy when selecting the date field?(Only Business days should be selected)

Akhil42
Tera Contributor

How to restrict saturday and sunday using UI Policy when selecting the date field?(Only Business days should be selected)

1 ACCEPTED SOLUTION

I think you should mark this response as correct as that contains the script

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

View solution in original post

15 REPLIES 15

Hi,

something like this for onChange of End Date

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

	checkDate: function(){

		var startDate = new GlideDateTime(this.getParameter('sysparm_startDate'));
		var endDate = new GlideDateTime(this.getParameter('sysparm_endDate'));
		var days = this.getDateDiffExcWeekends(startDate,endDate);
		if(days > 3)
			return 'valid';
		else
			return 'invalid';
	},

	getDateDiffExcWeekends: function(start,end){
		var days = 0;
		while (start < end) {
			start.addDaysUTC(1);
			if (start.getDayOfWeekUTC() != 6 && start.getDayOfWeekUTC() != 7){
				days++ ;
			}
		}
		return days+1;
	},

	type: 'ValidateDate'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	var ga = new GlideAjax("ValidateDate");
	ga.addParam("sysparm_name","checkDate");
	ga.addParam("sysparm_endDate", g_form.getValue('end_date'));
	ga.addParam("sysparm_startDate", g_form.getValue('start_date'));
	var response = ga.getXMLAnswer(parseResponse);
	function parseResponse(answer) {
		if(answer == 'invalid') {
			g_form.showFieldMsg('end_date','Select any weekday','error',true);
		}
	}
}

Regards
Ankur

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

Akhil42
Tera Contributor

find_real_file.pngfind_real_file.pngfind_real_file.png

Not working .Please refer these Attachments

Hi,

your start date is 21st April

end date is 28th april means end date is more than 3 days than 21st april excluding weekends

so no error message; it means it is working fine

what is not working?

Regards
Ankur

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

Hi

From 21st to 28th there are more than 3 working days,it should throw an error 

Hi,

so you want this

1) when the difference is more than 3 then invalid

2) else valid

update as this

if(days > 3)
            return 'invalid';
        else
            return 'valid';

Regards
Ankur

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