- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2020 09:13 AM
We have a need to require a requested_completion_date to be at least 5 business days, if you select a date less than 5 business days you get an error and it clears the field. My question is do I have to write a script include with this or can I write an onChange client script to accomplish this? The schedule we use is '8-8 weekdays excluding holidays'.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2020 08:53 AM
Resolved:
onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Call the GA function
var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getInfo");
ga.addParam('sysparm_date', g_form.getValue('requested_completion_date'));
ga.getXMLAnswer(function(answer){
if(answer == 'true'){
alert('Please select date after 5 business days');
g_form.clearValue('requested_completion_date');
}
});
}
Script Include:
var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getInfo: function(){
var dateSelected = this.getParameter('sysparm_date');
var nowDateTime = new GlideDateTime();
var days = 5;
//var dur = new GlideDuration(60*60*24*days*1000);
var dur = new GlideDuration(days*43200*1000);
// paste the sys_id of the 8*8 weekday schedule excluding holidays
var schedule = new GlideSchedule('2218ff1bdba8eb40fb6e753a8c96198d'); //8-8 weekdays excluding holidays
var finalTime = schedule.add(nowDateTime, dur,'');
var updatedGdt = new GlideDateTime(dateSelected);
var finalTimeGdt = new GlideDateTime(finalTime);
if(updatedGdt < finalTimeGdt){
return 'true';
}
return 'false';
},
type: 'u_userInfoAjax'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2020 08:29 AM
Ankur - any ideas on this?
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Call the GA function
var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getInfo");
ga.addParam('sysparm_date', g_form.getValue('requested_completion_date'));
ga.getXMLAnswer(function(answer){
if(answer == 'true'){
alert('Please select date after 5 business days');
g_form.clearValue('requested_completion_date');
}
});
}
SCRIPT INCLUDE:
var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getInfo: function(){
var dateSelected = this.getParameter('sysparm_date');
var nowDateTime = new GlideDateTime();
var days = 5;
var dur = new GlideDuration(60*60*24*days*1000);
// paste the sys_id of the 8*8 weekday schedule excluding holidays and weekends
var schedule = new GlideSchedule('2218ff1bdba8eb40fb6e753a8c96198d');
var finalTime = schedule.add(nowDateTime, dur,'');
var updatedGdt = new GlideDateTime(dateSelected);
var finalTimeGdt = new GlideDateTime(finalTime);
if(updatedGdt < finalTimeGdt){
return 'true';
}
return 'false';
},
type: 'u_userInfoAjax'
});
Perhaps the calculation of time is off because we use a 12 hour schedule per day vs an 8 or 9 hour?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2022 04:52 PM
I marked your answer as helpful. Thank you, the code worked perfectly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2020 10:02 AM
Hi Booher04,
I have figured out a code on Onchange client script. which is calling a function from a Script Include using GlideAjax
Client Scipt:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var cdt = newValue; //The date field
var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.
g_form.hideFieldMsg('due_date');
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
if(answer<0 || answer<5){
g_form.showFieldMsg('due_date', "Date should be more than 5 working days from today " , 'error');
//alert('Change value');
// alert("Date should be more than 5 working days from today.");
g_form.clearValue('request_completion_date'); // Pass the variable name here
g_form.setMandatory('request_completion_date');
}
}
Script Include -Client Callable
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//Takes a Single Date/Time Field and returns its time difference from nowDateTime().
//params = sysparm_fdt (the first date/time field), sysparm_difftype (time based format to return result. See "_calcDateDiff" function comments)
getNowDateTimeDiff: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day
var diff = gs.dateDiff(gs.nowDateTime(), firstDT, true);
var timediff = this._calcDateDiff(diffTYPE, diff);
//return "getNowDateTimeDiff: FIRST DT: " + firstDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;
return timediff;
},
//Private function to calculate the date difference return result in second, minute, hour, day.
_calcDateDiff: function(diffTYPE, seconds){
var thisdiff;
if (diffTYPE == "day"){thisdiff = seconds/86400;}
else if (diffTYPE == "hour"){thisdiff = seconds/3600;}
else if (diffTYPE == "minute"){thisdiff = seconds/60;}
else if (diffTYPE == "second"){thisdiff = seconds;}
else {thisdiff = seconds;}
return thisdiff;
}
}
Mark ✅ Correct and 👍 Helpful based on the impact.
Warm Regards,
Arvind
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2020 11:48 AM
Hi Booher04,
Why don't you give a shot to code I posted.
Mark ✅ Correct and 👍 Helpful based on the impact.
Warm Regards,
Arvind
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2020 12:48 PM
It gives me an error when I try to save the Script Include.
Could not save record because of a compile error: JavaScript parse error at line (49) column (1) problem = missing ) after argument list (<refname>; line 49)