- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2018 10:57 AM
Hi Community!
I've got a date field in an order guide that I want to add a field message to if the value of this field is less than 7 business days in the future. I've got a catalog client script and a script include for this. Problem is that (seemingly) random dates in the future that are more than 7 business days are triggering the field message.
Notice in my screen shots that the message is showing for a date way out in May, but only on that day, and not the date 1 day before or after. Odd?? Anyone see a problem in my scripts below, or have a better way to achieve my goal? Is there an OOTB GlideDateTime API method that I could use instead? thanks!
Here's my client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
g_form.hideFieldMsg('needed_by_date');
if (isLoading || newValue === '') {
return;
}
/************* This Checks whether the selected date is within 7 business days ahead**********/
var ga = new GlideAjax('ClientDateTimeUtils');
ga.addParam ('sysparm_name','withinBusinessHours');
ga.addParam ('sysparm_hours','54');
ga.addParam ('sysparm_requestedDate',newValue);
ga.getXML(CheckScheduleAgainstDatesParse);
}
function CheckScheduleAgainstDatesParse (response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'False'){
g_form.showFieldMsg('needed_by_date','You are requesting a new user setup too close to their scheduled start date. Please be advised that IT equipment cannot be guaranteed on their first day.','error');
}
}
and here's my script include "withinBusinessHours" function:
withinBusinessHours: function() {
var retVal; // Return value
var pn = this.getParameter('sysparm_hours');
var requestedDate= this.getParameter('sysparm_requestedDate');
var reqDateTime = new GlideDateTime(requestedDate);
gs.log(reqDateTime);
if(pn > 0){
//Get a schedule by sys_id to calculate duration
var schedule = new GlideSchedule();
schedule.load('090eecae0a0a0b260077e1dfa71da828');
//Get the current date/time in correct format for duration calculation
var actualDateTime = new GlideDateTime();
actualDateTime.setDisplayValue(gs.nowDateTime());
//Create variable to capture the date we want to compare against
//requestedDate = new GlideDateTime("2017-06-16 00:00:00");
//Date difference calculated based on specified schedule
var difDay = schedule.duration(actualDateTime,reqDateTime).getDayPart()*24;
var difHour = schedule.duration(actualDateTime,reqDateTime).getDurationValue().split(':')[0].substr(-2);
var dif = difDay + parseInt(difHour.replace(/^[0]+/g,""));
if(dif >= pn){
retVal = "True";
} else {
retVal = "False";
}
gs.log(retVal);
return retVal;
}
},
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2018 04:42 PM
No. It was parseInt. Use the code I just posted. It should be fine now.
withinBusinessHours: function() {
var retVal; // Return value
var pn = Number(this.getParameter('sysparm_hours'));
var requestedDate= this.getParameter('sysparm_requestedDate');
var reqDateTime = new GlideDateTime(requestedDate);
gs.log(reqDateTime);
if(pn > 0){
//Get a schedule by sys_id to calculate duration
var schedule = new GlideSchedule();
schedule.load('090eecae0a0a0b260077e1dfa71da828');
//Get the current date/time in correct format for duration calculation
var actualDateTime = new GlideDateTime();
actualDateTime.setDisplayValue(gs.nowDateTime());
//Create variable to capture the date we want to compare against
//requestedDate = new GlideDateTime("2017-06-16 00:00:00");
//Date difference calculated based on specified schedule
var difDay = schedule.duration(actualDateTime,reqDateTime).getDayPart()*24;
var difHour = schedule.duration(actualDateTime,reqDateTime).getDurationValue().split(':')[0].substr(-2);
gs.log('+++++++++++++actualDateTime+++++++++++'+actualDateTime );
gs.log('+++++++++++++reqDateTime+++++++++++'+reqDateTime );
gs.log('+++++++++++++difDay +++++++++++'+difDay );
gs.log('+++++++++++++difHour+++++++++++'+difHour);
var dif = Number(difDay + Number(difHour));
gs.log('++++++++++++++++++Total Hours ++++++++++'+ dif);
gs.log('++++++++++++++++++hours to compare ++++++++++'+ pn);
if(dif >= pn){
retVal = "True";
} else {
retVal = "False";
}
gs.log(retVal);
return retVal;
}
},
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2018 03:56 PM
Ok. Just need to add a parseInt
withinBusinessHours: function() {
var retVal; // Return value
var pn = parseInt(this.getParameter('sysparm_hours'));
var requestedDate= this.getParameter('sysparm_requestedDate');
var reqDateTime = new GlideDateTime(requestedDate);
gs.log(reqDateTime);
if(pn > 0){
//Get a schedule by sys_id to calculate duration
var schedule = new GlideSchedule();
schedule.load('090eecae0a0a0b260077e1dfa71da828');
//Get the current date/time in correct format for duration calculation
var actualDateTime = new GlideDateTime();
actualDateTime.setDisplayValue(gs.nowDateTime());
//Create variable to capture the date we want to compare against
//requestedDate = new GlideDateTime("2017-06-16 00:00:00");
//Date difference calculated based on specified schedule
var difDay = schedule.duration(actualDateTime,reqDateTime).getDayPart()*24;
var difHour = schedule.duration(actualDateTime,reqDateTime).getDurationValue().split(':')[0].substr(-2);
gs.log('+++++++++++++actualDateTime+++++++++++'+actualDateTime );
gs.log('+++++++++++++reqDateTime+++++++++++'+reqDateTime );
gs.log('+++++++++++++difDay +++++++++++'+difDay );
gs.log('+++++++++++++difHour+++++++++++'+difHour);
var dif = parseInt(difDay + parseInt(difHour));
gs.log('++++++++++++++++++Total Hours ++++++++++'+ dif);
gs.log('++++++++++++++++++hours to compare ++++++++++'+ pn);
if(dif >= pn){
retVal = "True";
} else {
retVal = "False";
}
gs.log(retVal);
return retVal;
}
},
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2018 03:56 PM
so it's not working with that either...just seems to be randomly working and not working
here's my full script again
var WithinBusinessDays = Class.create();
WithinBusinessDays.prototype = Object.extendsObject(AbstractAjaxProcessor, {
withinBusinessHours: function() {
var retVal; // Return value
var pn = this.getParameter('sysparm_hours');
var requestedDate= this.getParameter('sysparm_requestedDate');
var reqDateTime = new GlideDateTime(requestedDate);
gs.log('++++'+reqDateTime);
if(pn > 0){
//Get a schedule by sys_id to calculate duration
var schedule = new GlideSchedule();
schedule.load('090eecae0a0a0b260077e1dfa71da828');
//Get the current date/time in correct format for duration calculation
var actualDateTime = new GlideDateTime();
actualDateTime.setDisplayValue(gs.nowDateTime());
//Create variable to capture the date we want to compare against
//requestedDate = new GlideDateTime("2017-06-16 00:00:00");
//Date difference calculated based on specified schedule
var difDay = schedule.duration(actualDateTime,reqDateTime).getDayPart()*24;
var difHour = schedule.duration(actualDateTime,reqDateTime).getDurationValue().split(':')[0].substr(-2);
gs.log('++++actualDateTime+++++++++++'+actualDateTime );
gs.log('++++reqDateTime+++++++++++'+reqDateTime );
gs.log('++++difDay +++++++++++'+difDay );
gs.log('++++difHour+++++++++++'+difHour);
var dif = difDay + parseInt(difHour.replace(/^[0]+/g,""));
//var dif = difDay + difHour;
gs.log('++++Total Hours ++++++++++'+dif);
gs.log('++++hours to compare ++++++++++'+pn);
if(dif >= pn){
retVal = "True";
} else {
retVal = "False";
}
gs.log('++++'+retVal);
return retVal;
}
},
type: 'WithinBusinessDays'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2018 04:12 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2018 04:15 PM
sorry 4.25.18 returned false, but 4.26.18 returns true.
I think there's something up with SN's GlideDateTime?? We just upgraded to Jakarta, but this bug was happening with my original script back in Helsinki, and now again with Jakarta. Here's the log result with 4.26.18, as the date, looks good. But with 4.25.18 it got that "NaN" result and seems to break it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2018 04:18 PM