- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2021 03:28 AM
How to restrict saturday and sunday using UI Policy when selecting the date field?(Only Business days should be selected)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-23-2021 06:02 AM
I think you should mark this response as correct as that contains the script
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2021 04:03 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2021 04:40 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2021 05:37 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2021 06:11 AM
Hi
From 21st to 28th there are more than 3 working days,it should throw an error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2021 06:36 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
