asifnoor
Kilo Patron

Hi,

Earlier in my article, i have shown you how we can restrict the user to select only weekdays. In general, even in weekdays, there could be specific business times (such as 9-5) and also holidays which we want to exclude from the selection.

In this article, i will show you how we can use the schedule feature of SN and can restrict user to not select any date/time which is not in the schedule.

For this, we will use the GlideSchedule API of ServiceNow. Using Schedules, you can add your own list of holidays, mention the timings of your working environment. 

SN by default provides a list of standard schedules. you can either modify them or create a new one.

 

For this we need 2 scripts.

1. Client Script: To call the SI to check if the selected date is in scheudle or not

2. Script Include: Which captures the date and check if the date is in schedule and return true/false.

Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   //Show error if selected date/time is not in schedule.
   var ga = new GlideAjax("CheckSchedule");
   ga.addParam("sysparm_name","isInSchedule");
   ga.addParam("sysparm_date",newValue);
   var response = ga.getXMLAnswer(parseResponse);
    function parseResponse(answer) {
	if(answer == 'false') {
		g_form.showFieldMsg('expected_start','Select the date/time which is in schedule','error',true);
	}
    }
}

 

Script Include

Name: CheckSchedule

ClientCallable: True

var CheckSchedule = Class.create();
CheckSchedule.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isInSchedule : function() {
		var selected_date = this.getParameter("sysparm_date");
		gs.log("Date is "+selected_date);
		var d = new GlideDateTime();
		d.setDisplayValue(selected_date);
                //mention your schedule sys_id here.
		var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); 
		if(schedule.isInSchedule(d)) {
			return true;
		} else {
			return false;
		}
	},
    type: 'CheckSchedule'
});

Let me know if you have any questions in the comments below.

Mark the article as helpful and bookmark if you found it useful.

Comments
Vikas-Malhotra
Mega Guru

Nice article Asifnoor. Thanks for sharing. Much appreciated.

 

Thanks!

Vikas

asifnoor
Kilo Patron

Glad you like it.

Meenal Gharat
Giga Guru

Hi asifnoor,

Some of the script is causing the below error.any information or suggestions on the below error why it can gets trigger?

ScheduleSpanCalculator.getNextDate loop (fCurDay=null; fRangeStart=java.util.GregorianCalendar[time=1595746800000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Phoenix",offset=-25200000,dstSavings=0,useDaylight=false,transitions=12,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2020,MONTH=6,WEEK_OF_YEAR=31,WEEK_OF_MONTH=5,DAY_OF_MONTH=26,DAY_OF_YEAR=208,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=-25200000,DST_OFFSET=0]; in time span '04-03-0198 14:00:44 - 04-10-0198 14:00:43 America/Phoenix repeats [Every 7 day]

Thanks and Regards,

Meenal

asifnoor
Kilo Patron

Hi,

Did you make any chnages to the script in your code? If yes kindly share your code and i can check and help you.

raja31
Tera Contributor

Can i know what are the field name that you used here

thank you

Lieve Verhelst
Tera Expert

Hi,

Hoping you might see what I'm doing wrong/missing here...;

We have a default schedule for Weekdays 8 - 18h with a child schedule that is excluding public holidays.

find_real_file.png

parent schedule sys-id: 64d46d2adb3a3b8016f1b6bffe9619cb

child schedule sys-id: 236faeb0db4c4c1016f1b6bffe9619e0

Script include: BECheckSchedule 

Client Callable: True

 var BECheckSchedule = Class.create();
BECheckSchedule.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isInSchedule : function() {
		var selected_date = this.getParameter("sysparm_date")+'12:00:00';
		gs.log("Date is "+selected_date);
		var d = new GlideDateTime();
		d.setDisplayValue(selected_date);
                //mention your schedule sys_id here.
		var schedule = new GlideSchedule('64d46d2adb3a3b8016f1b6bffe9619cb'); 
		if(schedule.isInSchedule(d)) {
			return true;
		} else {
			return false;
		}
	},

    type: 'BECheckSchedule'
});

 

Client script: 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   //Show error if selected date/time is not in schedule.
   var ga = new GlideAjax("BECheckSchedule");
   ga.addParam("sysparm_name","isInSchedule");
   ga.addParam("sysparm_date",newValue);
   var response = ga.getXMLAnswer(parseResponse);
    function parseResponse(answer) {
//troubleshooting alert(answer);
	if(answer == 'false') {
		g_form.clearValue('date_of_visit_at_client');
		g_form.showFieldMsg('date_of_visit_at_client','Please select a weekday - non public holiday','error',true);
	}
    }
}

 

gs.log has the correct date

When I uncomment my alert in client script, I always get the answer False. doesn't matter what date I select, no idea what to do or where to look anymore. 

asifnoor
Kilo Patron

I think when you use it converts the date/time format.

		d.setDisplayValue(selected_date);

can you try setValue and test once and check if its in the schedule or in child schedule or not.

Uma2
Tera Expert

Hi @asifnoor 

 

 Could you help me to convert the date format.

I want to allow the date should be after 5 business days.

Selected date and system date are different formates.

Uma2_0-1672129979758.png

 

asifnoor
Kilo Patron

You need to first convert using getByFormat and setDisplayValue to the system format.

newhand
Mega Sage

HI  @asifnoor 

Thank you for your shell。

 

In many systems, the usual effect is that non-business dates cannot be selected directly from the date panel.

Do you know will snow provide similar date picker? I think it will make snow more powerful and infact many of our clients need the date picker works like that。

 

It is a very unpleasant practice to check the date and give an error message through onchange,because the user may doesn't know what date can he select.....The experience is not very good。

 

 

Version history
Last update:
‎01-13-2020 10:24 PM
Updated by: