- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2017 08:28 AM
Hi helpful friends!
reposting this...I've gotten help already (thanks guys!) with several codes that I could not get to work properly and have gotten my head pretty spun around jumbling 3 different solutions, which again unfortunately I could not get to work. Looking to start fresh. Seeing lots of entries in the community on this but with the wide degree of differences in restrictions people are shooting for I'm having difficulty extrapolating.
I have a date field on a catalog item that I need to put restraints on.
I need it to only allow an entry of a date 6 or more BUSINESS days in the future.
the name of my date field is "needed_by"
Any help is most appreciated!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2017 09:13 AM
Hi Patrick,
You need to write the onChange Catalog Client Script on change of the variable named 'needed_by' and call a Script include where the validations are done. Please find the scripts for that here. Please refer my response on this thread: Issue related to Date/Time variable
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
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.
/******** This Checks whether the selected date is in the Past or Not************************/
var ajax = new GlideAjax('ClientDateTimeUtils_New');
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");
if(answer<0){
alert("Date & Time should not be Prior to Current Date & Time");
g_form.clearValue('date'); // Pass the variable name here
g_form.setMandatory('date', true);
}
}
/************* This Checks whether the selected date is within 6 business day ahead & Not weekend**********/
var ga = new GlideAjax('ClientDateTimeUtils_New');
ga.addParam ('sysparm_name','isBusinessDay');
ga.addParam ('sysparm_schedule_name','8-5 weekdays');
ga.addParam ('sysparm_dateTime',newValue);
ga.getXML(CheckScheduleAgainstDatesParse);
function CheckScheduleAgainstDatesParse (response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(parseInt(answer)>63){ //For 6 days with 9 hour per day
alert("Date should be within 6 business days & within business working hours");
g_form.clearValue('date');
g_form.setMandatory('date', true);
}
}
}
Script Include:
var ClientDateTimeUtils_New = Class.create();
ClientDateTimeUtils_New.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 diff;
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
diff = gs.dateDiff(gs.nowDateTime(), firstDT, true);
var timediff = this._calcDateDiff(diffTYPE, diff);
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;
},
//Takes a GlideDate field and schedule
//Returns true if the date falls within the given schedule and within today+6 Business days, false otherwise.
isBusinessDay: function (){
var sched;
var rslt;
var dur = 64;
var schedname = this.getParameter('sysparm_schedule_name');
var date_time = this.getParameter('sysparm_dateTime');
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', schedname);
//automatically use correct API for instance build
if (typeof GlideSchedule != 'undefined')
sched = new GlideSchedule(schedRec.sys_id);
else
sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);
//Get the date/time in correct format
var DateTime = new GlideDateTime();
DateTime.setDisplayValue(date_time);
rslt = sched.isInSchedule(DateTime);
if (rslt){
var myUserTZ = gs.getUser().getTZ();
var dc = new DurationCalculator();
dc.setSchedule(schedRec.sys_id);
dc.setTimeZone(myUserTZ);
dur = dc.calcScheduleDuration(gs.now(), date_time)/3600;
}
return dur.toString();
}
});
I hope this helps.Please mark correct/helpful based on impact
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2017 09:55 AM
ok thanks, so if I'm not using a custom schedule, then I leave this line as is?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2017 09:59 AM
Hi Patrick,
Yes, you can. Until you don't have any issue with it's Schedule Entries and Child Schedules. Besides using it in your script, I would like to encourage you to have a look at the Schedule followed by the path which I have mentioned in my last response.
I hope this helps.Please mark correct/helpful based on impact
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2017 10:20 AM
ok I looked in there and did find a schedule to use and have put it in the code.
Do I need to replace the "date" in the script include also?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2017 10:27 AM
Hi Patrick,
Nope, that's good to go. Were you able to try?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2017 10:32 AM
Hi Amlan,
so here's what I've got, so far nothing is happening, no alerts, any date can be entered in the field. I must have something wrong. the name of my field is "needed_by"
catalog client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
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.
/******** This Checks whether the selected date is in the Past or Not************************/
var ajax = new GlideAjax('ClientDateTimeUtils_New');
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");
if(answer<0){
alert("Date should not be Prior to Current Date & Time");
g_form.clearValue('needed_by'); // Pass the variable name here
g_form.setMandatory('needed_by', true);
}
}
/************* This Checks whether the selected date is within 6 business day ahead & Not weekend**********/
var ga = new GlideAjax('ClientDateTimeUtils_New');
ga.addParam ('sysparm_name','isBusinessDay');
ga.addParam ('sysparm_schedule_name','US Business Hours Calendar');
ga.getXML(CheckScheduleAgainstDatesParse);
function CheckScheduleAgainstDatesParse (response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(parseInt(answer)>63){ //For 6 days with 9 hour per day
alert("Date should be within 6 business days & within business working hours");
g_form.clearValue('needed_by');
g_form.setMandatory('needed_by', true);
}
}
}
and my script include is yours unchanged. name is "ClientDateTimeUtils"