- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 07:34 AM
I have a date/time field that I need to validate if the date/time fall within business hours for a specific schedule that is US/Eastern timezone. If the person is in a number of different regions I need it to consider this so it always is comparing to the US/Eastern time.
Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var dateStr = new Date(newValue);
var ga = new GlideAjax('global.ValidateBusinessHours');
ga.addParam('sysparm_name', 'isInBusinessHours');
ga.addParam('dateTimeStr', dateStr);
ga.getXMLAnswer(getResponse);
function getResponse(response){
if (response == true){
alert("Success");
} else {
alert("Please select a date within business hours (M-F 8-5pm EST)");
}
}
}
Script Include
var ValidateBusinessHours = Class.create();
ValidateBusinessHours.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isInBusinessHours: function() {
var dateTimeStr = this.getParameter('dateTimeStr');
var dateTime = new GlideDateTime(dateTimeStr);
// Retrieve the business schedule for the user's location
var businessSchedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae','GMT');
// Check if the provided date and time falls within business hours
if (businessSchedule.isInSchedule(dateTime)){
return true;
} else { return false;}
},
type: 'ValidateBusinessHours'
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2024 07:56 AM
I was able to solve the issue, the problem seems to have been that the GMT was then being used as the "current" time when creating the new date variable and needed to convert for the correct time zone.
var ValidateBusinessHours = Class.create();
ValidateBusinessHours.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isInBusinessHours: function() {
var dateTimeStr = this.getParameter('dateTimeStr');
var dateTime = new GlideDateTime();
dateTime.setDisplayValue(dateTimeStr);
var userTZ = gs.getUser().getTZ();
// Retrieve the business schedule for the user's location
var businessSchedule = new GlideSchedule();
businessSchedule.load('157ce14a1b1dc65044f654ae034bcb59');
businessSchedule.setTimeZone(userTZ);
// Check if the provided date and time falls within business hours
if (businessSchedule.isInSchedule(dateTime)){
return true;
} else { return false;}
},
type: 'ValidateBusinessHours'
});
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('global.ValidateBusinessHours');
ga.addParam('sysparm_name', 'isInBusinessHours');
ga.addParam('dateTimeStr', newValue);
ga.getXMLAnswer(getResponse);
function getResponse(response) {
if (response == 'true') {
//alert("Success");
} else {
alert("Please select a date within business hours (M-F 8-5pm EST)");
g_form.clearValue('date_time_secondary');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 07:41 AM
@MichaelH5716707 Ensure that the input date/time is converted to the US/Eastern timezone before performing any comparisons with business hours. The script should also check if the provided date and time fall within the business hours of Monday to Friday, 8 am to 5 pm Eastern Standard Time (EST).
Client Script:
// Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var dateStr = new Date(newValue);
// Convert date to UTC string to avoid timezone discrepancies
var utcDateStr = dateStr.toISOString();
var ga = new GlideAjax('global.ValidateBusinessHours');
ga.addParam('sysparm_name', 'isInBusinessHours');
ga.addParam('dateTimeStr', utcDateStr);
ga.getXMLAnswer(getResponse);
function getResponse(response){
if (response == 'true'){
alert("Success");
} else {
alert("Please select a date within business hours (M-F 8-5pm EST)");
}
}
}
Script Include:
// Script Include
var ValidateBusinessHours = Class.create();
ValidateBusinessHours.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isInBusinessHours: function() {
var dateTimeStr = this.getParameter('dateTimeStr');
// Create a GlideDateTime object and set timezone to US/Eastern
var dateTime = new GlideDateTime();
dateTime.setDisplayValue(dateTimeStr);
dateTime.setTZ('America/New_York');
// Check if it's a weekday and within business hours (8 am - 5 pm EST)
if (this.isWeekday(dateTime) && this.isBusinessHours(dateTime)){
return 'true';
} else {
return 'false';
}
},
isWeekday: function(dateTime) {
// Check if the day is between Monday (1) and Friday (5)
var dayOfWeek = dateTime.getDayOfWeek();
return (dayOfWeek >= 2 && dayOfWeek <= 6); // 2: Monday, 6: Friday
},
isBusinessHours: function(dateTime) {
// Check if the time is between 8 am (08:00) and 5 pm (17:00)
var hour = dateTime.getHourOfDay();
return (hour >= 8 && hour < 17);
},
type: 'ValidateBusinessHours'
});
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 08:13 AM
Thanks for the quick response this doesn't appear to be what I am looking for or at least isn't acting as expected. I am needing a person to put in their local time so for example I am in Arizona so I will see the date/time and select for example my local m-f at 4pm. I need the script to look and see if that time is outside of business hours which currently I was trying to call to the business schedule for that because it will also evaluate holidays. If it lands outside of those business hours which are localized to eastern timezone then it will give an error. I implemented your suggestion but isn't validating correctly. It seems to always fire as outside of business hours.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2024 03:35 PM
Hey @MichaelH5716707,
See if this works:
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('global.ValidateBusinessHours');
ga.addParam('sysparm_name', 'isInBusinessHours');
ga.addParam('dateTimeStr', newValue);
ga.getXMLAnswer(getResponse);
function getResponse(response) {
if (response == 'true') {
alert("Success");
} else {
alert("Please select a date within business hours (M-F 8-5pm EST)");
}
}
}
Script Include:
var ValidateBusinessHours = Class.create();
ValidateBusinessHours.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isInBusinessHours: function() {
var dateTimeStr = this.getParameter('dateTimeStr') + "";
var dateTime = new GlideDateTime();
dateTime.setDisplayValue(dateTimeStr);
var businessSchedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
if (businessSchedule.isInSchedule(dateTime)) {
return true;
} else {
return false;
}
},
type: 'ValidateBusinessHours'
});
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2024 07:56 AM
I was able to solve the issue, the problem seems to have been that the GMT was then being used as the "current" time when creating the new date variable and needed to convert for the correct time zone.
var ValidateBusinessHours = Class.create();
ValidateBusinessHours.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isInBusinessHours: function() {
var dateTimeStr = this.getParameter('dateTimeStr');
var dateTime = new GlideDateTime();
dateTime.setDisplayValue(dateTimeStr);
var userTZ = gs.getUser().getTZ();
// Retrieve the business schedule for the user's location
var businessSchedule = new GlideSchedule();
businessSchedule.load('157ce14a1b1dc65044f654ae034bcb59');
businessSchedule.setTimeZone(userTZ);
// Check if the provided date and time falls within business hours
if (businessSchedule.isInSchedule(dateTime)){
return true;
} else { return false;}
},
type: 'ValidateBusinessHours'
});
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('global.ValidateBusinessHours');
ga.addParam('sysparm_name', 'isInBusinessHours');
ga.addParam('dateTimeStr', newValue);
ga.getXMLAnswer(getResponse);
function getResponse(response) {
if (response == 'true') {
//alert("Success");
} else {
alert("Please select a date within business hours (M-F 8-5pm EST)");
g_form.clearValue('date_time_secondary');
}
}
}