
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2020 07:27 AM
Requirement:
we have two date variables (due_date and disable_date) that have specific rules around them. The dates can not include weekends or holidays (Schedule: Weekdays excluding holidays - 24 hours - 5b1d15ffdsdc9cd087047e88f4961928) and if the date is less than 15 days after the current (opening) date, the user is presented with a mandatory 'justification' multi-line text variable.
i tried accomplishing this using the below onChange client script, but ran into issues, where depending on the time of day, it blocked Fri/Sat instead of Sat/Sun and could not capture holidays of course.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//get the new and current date/time as an object
var dateObjectNow = new Date();
var dateObjectNew = new Date(newValue);
//get the dates in days - also use floor to convert valeus to integers
dateNow = Math.floor(dateObjectNow.valueOf() / (1000 * 60 * 60 * 24));
dateNew = Math.floor(dateObjectNew.valueOf() / (1000 * 60 * 60 * 24));
// Get day of week (Sunday = 0)
var dayOfWeek = dateObjectNew.getDay();
var msg;
if (dayOfWeek > 4) {
msg = 'ERROR: Date cannot be on the weekend.';
g_form.hideFieldMsg('due_date', true);
g_form.showFieldMsg('due_date', msg, 'error', false);
g_form.setValue('due_date', '');
}
}
Any magic that you can assist me with or guide me to, as my search on Community has not been fruitful (probably due to keywords), is greatly appreciated.
Solved! Go to Solution.
- Labels:
-
Multiple Versions
-
Service Catalog

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2021 10:14 AM
We were able to accomplish this with the creation of an additional field (calculated_due_date), a few catalog client scripts, and a Script Include
Catalog Client Script: Prepopulate Due and Disable Dates
function onChange(control, oldValue, newValue, isLoading) {
/* if (isLoading) {
return;
}
*/
//Reset variables
g_form.setVisible('calculated_due_date', false);
g_form.setMandatory('justification', false);
g_form.setVisible('justification', false);
g_form.setMandatory('disable_justification', false);
g_form.setVisible('disable_justification', false);
//Calculate due_date field
var ga = new GlideAjax('SentaraScheduleUtils');
ga.addParam('sysparm_name', 'getDatePlusXDays'); //sched util function
ga.addParam('sysparm_date', newValue); // current date
ga.addParam('sysparm_numDaysToAdd', 15); // add 15 days
ga.addParam('sysparm_useExactTime', 'false');
ga.addParam('sysparm_schedule', 'ddf2666c1b642050e32192c8af4bcb32'); //Sentara weekdays excluding holidays - 24 hour
ga.getXML(dateParse);
}
function dateParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('due_date', answer);
g_form.setValue('disable_date', answer);
g_form.setValue('calculated_due_date', answer);
}
Catalog Client Script: Validate Due Date
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setMandatory('justification', false);
g_form.setVisible('justification', false);
return;
} else {
//get original calculated due date
var origDueDate = g_form.getValue('calculated_due_date');
//Validate user date is a business day
//then checks if user date is sooner than the SLA (15 days) and shows/hides justification
validateNewDate(newValue);
}
}
function validateNewDate(newDate) {
//Validates that user date is a business day
var ga = new GlideAjax('SentaraScheduleUtils');
ga.addParam('sysparm_name', 'checkIfDateInSched'); //function name
ga.addParam('sysparm_date', newDate); //new date to validate
ga.addParam('sysparm_useExactTime', 'false');
ga.addParam('sysparm_schedule', 'ddf2666c1b642050e32192c8af4bcb32'); //Sentara weekdays excluding holidays - 24 hour
ga.getXML(valDateParse);
}
function valDateParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
var orig_Due_Date = g_form.getValue('calculated_due_date');
var new_Date = g_form.getValue('due_date'); //newValue is out of scope, so you have to use getValue to get it
checkDateDiff(orig_Due_Date, new_Date);
} else {
alert('The date you selected is not a business day or is prior to today, please select another day.');
g_form.setValue('due_date', g_form.getValue('calculated_due_date'));
g_form.setMandatory('justification', false);
g_form.setVisible('justification', false);
}
function checkDateDiff(orig_due_date, new_date) {
//getDateTimeDiff
var dttype = 'minute';
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'getDateTimeDiff');
ajax.addParam('sysparm_fdt', orig_due_date);
ajax.addParam('sysparm_sdt', new_date);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(checkDateParse);
}
function checkDateParse(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer < 0) {
g_form.setVisible('justification', true);
g_form.setMandatory('justification', true);
} else {
g_form.setMandatory('justification', false);
g_form.setVisible('justification', false);
}
}
}
Catalog Client Script: Validate Disable Date
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setMandatory('disable_justification', false);
g_form.setVisible('disable_justification', false);
return;
} else {
//get original calculated due date
var origDueDate = g_form.getValue('calculated_due_date');
//Validate user date is a business day
//then checks if user date is sooner than the SLA (15 days) and shows/hides justification
validateNewDate(newValue);
}
}
function validateNewDate(newDate) {
//Validates that user date is a business day
var ga = new GlideAjax('SentaraScheduleUtils');
ga.addParam('sysparm_name', 'checkIfDateInSched'); //function name
ga.addParam('sysparm_date', newDate); //new date to validate
ga.addParam('sysparm_useExactTime', 'false');
ga.addParam('sysparm_schedule', 'ddf2666c1b642050e32192c8af4bcb32'); //Sentara weekdays excluding holidays - 24 hour
ga.getXML(valDateParse);
}
function valDateParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
var orig_Due_Date = g_form.getValue('calculated_due_date');
var new_Date = g_form.getValue('disable_date'); //newValue is out of scope, so you have to use getValue to get it
checkDateDiff(orig_Due_Date, new_Date);
} else {
alert('The date you selected is not a business day or is prior to today, please select another day.');
g_form.setValue('disable_date', g_form.getValue('calculated_due_date'));
g_form.setMandatory('disable_justification', false);
g_form.setVisible('disable_justification', false);
//g_form.showFieldMsg('disable_date', 'The date you selected is not a business day or is prior to today, please select another day.', 'error');
}
function checkDateDiff(orig_due_date, new_date) {
//getDateTimeDiff
var dttype = 'minute';
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'getDateTimeDiff');
ajax.addParam('sysparm_fdt', orig_due_date);
ajax.addParam('sysparm_sdt', new_date);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(checkDateParse);
}
function checkDateParse(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer < 0) {
g_form.setVisible('disable_justification', true);
g_form.setMandatory('disable_justification', true);
} else {
g_form.setMandatory('disable_justification', false);
g_form.setVisible('disable_justification', false);
}
}
}
Script Include: SentaraScheduleUtils
var SentaraScheduleUtils = Class.create();
SentaraScheduleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDatePlusXDays: function(){ // adds days to date per calendar
//parameters
var user_date = this.getParameter('sysparm_date'); //start date
var useExactTime = this.getParameter('sysparm_useExactTime');
var days = this.getParameter('sysparm_numDaysToAdd'); //days to add
var sched_sysid = this.getParameter('sysparm_schedule'); //sys_id of Schedule to use
//calculate new date
var userDateAdj = user_date;
//if (!useExactTime){alert('use exact time invalid');}
if(useExactTime == 'false'){
//default to noon to avoid time zone issues with dates
userDateAdj = new GlideDateTime(user_date + ' 12:00:00');
}
var schedule = new GlideSchedule(sched_sysid, "US/Eastern");
var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
var end = schedule.add(userDateAdj, dur);
return end.getDate();// returns caclulated date
},
checkIfDateInSched: function(){ //determines if date is in the schedule
//parameters
var user_date = this.getParameter('sysparm_date'); //user date to check
var useExactTime = this.getParameter('sysparm_useExactTime');
var sched_sysid = this.getParameter('sysparm_schedule'); //sys_id of Schedule to use
//validate date
var userDateAdj = user_date;
if(useExactTime == 'false'){
userDateAdj = new GlideDateTime(user_date + ' 12:00:00');
}
var schedule = new GlideSchedule(sched_sysid, "US/Eastern");
if (schedule.isInSchedule(userDateAdj)){
return 'true'; // date is not excluded from the sched (typically a week day)
}else{//
return 'false'; //date is excluded (typically a holiday or weekend)
}
},
type: 'SentaraScheduleUtils'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2021 10:14 AM
We were able to accomplish this with the creation of an additional field (calculated_due_date), a few catalog client scripts, and a Script Include
Catalog Client Script: Prepopulate Due and Disable Dates
function onChange(control, oldValue, newValue, isLoading) {
/* if (isLoading) {
return;
}
*/
//Reset variables
g_form.setVisible('calculated_due_date', false);
g_form.setMandatory('justification', false);
g_form.setVisible('justification', false);
g_form.setMandatory('disable_justification', false);
g_form.setVisible('disable_justification', false);
//Calculate due_date field
var ga = new GlideAjax('SentaraScheduleUtils');
ga.addParam('sysparm_name', 'getDatePlusXDays'); //sched util function
ga.addParam('sysparm_date', newValue); // current date
ga.addParam('sysparm_numDaysToAdd', 15); // add 15 days
ga.addParam('sysparm_useExactTime', 'false');
ga.addParam('sysparm_schedule', 'ddf2666c1b642050e32192c8af4bcb32'); //Sentara weekdays excluding holidays - 24 hour
ga.getXML(dateParse);
}
function dateParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('due_date', answer);
g_form.setValue('disable_date', answer);
g_form.setValue('calculated_due_date', answer);
}
Catalog Client Script: Validate Due Date
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setMandatory('justification', false);
g_form.setVisible('justification', false);
return;
} else {
//get original calculated due date
var origDueDate = g_form.getValue('calculated_due_date');
//Validate user date is a business day
//then checks if user date is sooner than the SLA (15 days) and shows/hides justification
validateNewDate(newValue);
}
}
function validateNewDate(newDate) {
//Validates that user date is a business day
var ga = new GlideAjax('SentaraScheduleUtils');
ga.addParam('sysparm_name', 'checkIfDateInSched'); //function name
ga.addParam('sysparm_date', newDate); //new date to validate
ga.addParam('sysparm_useExactTime', 'false');
ga.addParam('sysparm_schedule', 'ddf2666c1b642050e32192c8af4bcb32'); //Sentara weekdays excluding holidays - 24 hour
ga.getXML(valDateParse);
}
function valDateParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
var orig_Due_Date = g_form.getValue('calculated_due_date');
var new_Date = g_form.getValue('due_date'); //newValue is out of scope, so you have to use getValue to get it
checkDateDiff(orig_Due_Date, new_Date);
} else {
alert('The date you selected is not a business day or is prior to today, please select another day.');
g_form.setValue('due_date', g_form.getValue('calculated_due_date'));
g_form.setMandatory('justification', false);
g_form.setVisible('justification', false);
}
function checkDateDiff(orig_due_date, new_date) {
//getDateTimeDiff
var dttype = 'minute';
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'getDateTimeDiff');
ajax.addParam('sysparm_fdt', orig_due_date);
ajax.addParam('sysparm_sdt', new_date);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(checkDateParse);
}
function checkDateParse(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer < 0) {
g_form.setVisible('justification', true);
g_form.setMandatory('justification', true);
} else {
g_form.setMandatory('justification', false);
g_form.setVisible('justification', false);
}
}
}
Catalog Client Script: Validate Disable Date
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setMandatory('disable_justification', false);
g_form.setVisible('disable_justification', false);
return;
} else {
//get original calculated due date
var origDueDate = g_form.getValue('calculated_due_date');
//Validate user date is a business day
//then checks if user date is sooner than the SLA (15 days) and shows/hides justification
validateNewDate(newValue);
}
}
function validateNewDate(newDate) {
//Validates that user date is a business day
var ga = new GlideAjax('SentaraScheduleUtils');
ga.addParam('sysparm_name', 'checkIfDateInSched'); //function name
ga.addParam('sysparm_date', newDate); //new date to validate
ga.addParam('sysparm_useExactTime', 'false');
ga.addParam('sysparm_schedule', 'ddf2666c1b642050e32192c8af4bcb32'); //Sentara weekdays excluding holidays - 24 hour
ga.getXML(valDateParse);
}
function valDateParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
var orig_Due_Date = g_form.getValue('calculated_due_date');
var new_Date = g_form.getValue('disable_date'); //newValue is out of scope, so you have to use getValue to get it
checkDateDiff(orig_Due_Date, new_Date);
} else {
alert('The date you selected is not a business day or is prior to today, please select another day.');
g_form.setValue('disable_date', g_form.getValue('calculated_due_date'));
g_form.setMandatory('disable_justification', false);
g_form.setVisible('disable_justification', false);
//g_form.showFieldMsg('disable_date', 'The date you selected is not a business day or is prior to today, please select another day.', 'error');
}
function checkDateDiff(orig_due_date, new_date) {
//getDateTimeDiff
var dttype = 'minute';
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'getDateTimeDiff');
ajax.addParam('sysparm_fdt', orig_due_date);
ajax.addParam('sysparm_sdt', new_date);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(checkDateParse);
}
function checkDateParse(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer < 0) {
g_form.setVisible('disable_justification', true);
g_form.setMandatory('disable_justification', true);
} else {
g_form.setMandatory('disable_justification', false);
g_form.setVisible('disable_justification', false);
}
}
}
Script Include: SentaraScheduleUtils
var SentaraScheduleUtils = Class.create();
SentaraScheduleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDatePlusXDays: function(){ // adds days to date per calendar
//parameters
var user_date = this.getParameter('sysparm_date'); //start date
var useExactTime = this.getParameter('sysparm_useExactTime');
var days = this.getParameter('sysparm_numDaysToAdd'); //days to add
var sched_sysid = this.getParameter('sysparm_schedule'); //sys_id of Schedule to use
//calculate new date
var userDateAdj = user_date;
//if (!useExactTime){alert('use exact time invalid');}
if(useExactTime == 'false'){
//default to noon to avoid time zone issues with dates
userDateAdj = new GlideDateTime(user_date + ' 12:00:00');
}
var schedule = new GlideSchedule(sched_sysid, "US/Eastern");
var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
var end = schedule.add(userDateAdj, dur);
return end.getDate();// returns caclulated date
},
checkIfDateInSched: function(){ //determines if date is in the schedule
//parameters
var user_date = this.getParameter('sysparm_date'); //user date to check
var useExactTime = this.getParameter('sysparm_useExactTime');
var sched_sysid = this.getParameter('sysparm_schedule'); //sys_id of Schedule to use
//validate date
var userDateAdj = user_date;
if(useExactTime == 'false'){
userDateAdj = new GlideDateTime(user_date + ' 12:00:00');
}
var schedule = new GlideSchedule(sched_sysid, "US/Eastern");
if (schedule.isInSchedule(userDateAdj)){
return 'true'; // date is not excluded from the sched (typically a week day)
}else{//
return 'false'; //date is excluded (typically a holiday or weekend)
}
},
type: 'SentaraScheduleUtils'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2022 05:51 AM