- 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'
});