Business Hours Validation

devendranarun
Tera Guru

Hi All,

Whenever i select time between 09:30 am and 06:30 pm, then an alert should pop up confirming on whether to go ahead as the time selected is within Business hours. For this, I have written Glide Ajax. The Client Script and Script Include are as follows;

However, this is not working as expected. It goes directly into the else loop.

Please advice on what changes do i need to do in the following scripts.

Timezone used is: IST

onChange Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || !isLoading) {
var ga = new GlideAjax('timeRange');
ga.addParam('sysparm_name','checkInSchedule');
ga.addParam('sysparm_sched_date', g_form.getValue('start_date'));
ga.getXML(checkInSchedule);
}
}

function checkInSchedule(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'true')
{
confirm('Time selected is within Business hours. Do you still want to continue?');
}
else if(answer == 'false')
{
alert(''Not within Business hours);
}
}

Script Include:

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

checkInSchedule: function()
{
var scheduled_date = this.getParameter('sysparm_sched_date');
var gdt = new GlideDateTime(scheduled_date);
var gdt1 = new GlideDateTime(gs.nowDateTime());
if((gdt > gdt1+" 09:30:00") && (gdt < gdt1+" 18:30:00"))
{
return true;
}
else
{
return false;
}
},
type: 'timeRange'
});

Any help is highly Appreciated!

 

2 REPLIES 2

Deepak Kumar5
Kilo Sage

checkInSchedule: function()
{
var scheduled_date = this.getParameter('sysparm_sched_date');
var gdt = new GlideDateTime(scheduled_date);
var gdt1 = new GlideDateTime(gs.nowDateTime());

var diff = gs.dateDiff(gdt, gdt1, false);


if((diff >"09:30:00") && (diff < "18:30:00"))
{
return true;
}
else
{
return false;
}
},

Lee Epps
Tera Guru

You might want to consider creating a Schedule (cmn_schedule) record to save your business hours. Then in your Script Include you can use the GlideSchedule api to check if the date is within the schedule. Like this: 

checkInSchedule: function()
{
	var scheduled_date = this.getParameter('sysparm_sched_date');
	var business_hours = new GlideSchedule([schedule sysID]);
	if(business_hours.isInSchedule(scheduled_date)) {
		return true;
	}
	else {
		return false;
	}
}

Plus with a Schedule, you can change the hours or add exceptions (e.g. for holidays) without updating your code.