- 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 04:28 PM
so far I've implemented this in our company DEV instance...could there be an issue with default date/time settings?
I'm going to build it in my personal developer instance and see if it works there.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2018 04:32 PM
Ok..Got the issue. try this
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 04:35 PM

- 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 04:57 PM
it's looking good Sanjiv, thanks so much for your help!