- 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 08:34 AM
Hi Patrick,
Hope the data in this link helps you.
Re: Setting up a Request for 10 Working Days in Service Catalog
Regards,
Sampath.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2017 08:53 AM
Hi Sampath,
I did see that post and have copied it exactly and it has not worked. I'll definitely revisit it and give it another go.
thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2017 09:17 AM
I faced the same issue long back Patrick.
Posting you a Catalog client script and a Script include, please modify it according to your requirement. I'm not sure but hope this helps you.
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('ClientDateTimeUtils10');
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('variable_name'); // Pass the variable name here
}
}
/************* This Checks whether the selected date is within 10 business day ahead & Not weekend**********/
var gaBusinessDay = new GlideAjax('ClientDateTimeUtils10');
gaBusinessDay.addParam('sysparm_name', 'isBusinessDay');
gaBusinessDay.addParam('sysparm_date', cdt);
gaBusinessDay.addParam('sysparm_difftype', dttype);
gaBusinessDay.addParam('sysparm_sched', '8-5 weekdays excluding holidays'); //Pass the schedule name here
gaBusinessDay.getXML(checkDateIsBusinessDay);
function checkDateIsBusinessDay(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'false'){
alert("Date should be within 10 business days & within business working hours");
g_form.clearValue('variable_name');
}
}
}
Script Include:
var ClientDateTimeUtils10 = Class.create();
ClientDateTimeUtils10.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+10 Business days, false otherwise.
isBusinessDay: function (){
var rslt = false;
var glide_date = new GlideDateTime();
var new_date = new GlideDateTime();
var date = this.getParameter('sysparm_date');
var timeZoneOffSet = glide_date.getTZOffset();
glide_date.setDisplayValue(date);
new_date.setDisplayValue(date);
glide_date.setNumericValue(glide_date.getNumericValue() + timeZoneOffSet);
//gs.addInfoMessage(glide_date);
var diffTYPE1 = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day
var day = new GlideDateTime();
var current_user_time = gs.nowDateTime();
day.setDisplayValue(current_user_time);
day.setNumericValue(day.getNumericValue() + timeZoneOffSet);
// The basis of our calculation
var dueDays = 10;
var dueWorkingHours = 9;
// The amount of time we want for the duration
var dueSeconds = dueDays*dueWorkingHours*60*60;
var leadTime = new GlideDuration(dueSeconds*1000);
var dueDateGdt;
var schedule = this.getParameter('sysparm_sched');
var schedRec = new GlideRecord('cmn_schedule');
if (schedRec.get('name', schedule)) {
var sched = new GlideSchedule(schedRec.sys_id);
dueDateGdt = sched.add(day, leadTime, '');
//gs.addInfoMessage("schedule exists");
}
//gs.addInfoMessage("This is the maximum date in GMT" + dueDateGdt);
dueDateGdt.setNumericValue(dueDateGdt.getNumericValue() + timeZoneOffSet);
//gs.addInfoMessage("This is the maximum date in TZ" + dueDateGdt);
var diff1 = gs.dateDiff(glide_date, dueDateGdt, true);
//gs.addInfoMessage("This is the difference" + diff1);
var timediff = this._calcDateDiff(diffTYPE1, diff1);
if(typeof GlideSchedule != 'undefined'){
var sched1 = new GlideSchedule(schedRec.sys_id);
if(sched1.isInSchedule(new_date) && timediff >= 0){
rslt = true;
} else{
rslt = false;
}
}
return rslt;
}
});
Thanks,
Sampath.
- 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