Problem with client script and script include comparing dates

patricklatella
Mega Sage

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!

find_real_file.png

find_real_file.png

find_real_file.png

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;

}

},

1 ACCEPTED SOLUTION

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.

View solution in original post

32 REPLIES 32

SanjivMeher
Kilo Patron
Kilo Patron

Run this and let me know what you see in the logs



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).dur.getByFormat('HH');



gs.log('+++++++++++++difDay +++++++++++'+difDay );


gs.log('+++++++++++++difHour+++++++++++'+difHour);


var dif = difDay + parseInt(difHour.replace(/^[0]+/g,""));



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.

Hi Sanjiv,


ok I actually created a new script include for just this function.   when plugging it in to the client script and changing the value of the date field a couple times I got this from the log



find_real_file.png


Some more correction. try this



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).getByFormat('HH');



gs.log('+++++++++++++difDay +++++++++++'+difDay );


gs.log('+++++++++++++difHour+++++++++++'+difHour);



var dif = difDay + parseInt(difHour.replace(/^[0]+/g,""));



gs.log('++++++++++++++++++Total Hours ++++++++++'+ diff);



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.

patricklatella
Mega Sage

might help to know the date entered...here's another one with corresponding log results



find_real_file.png


find_real_file.png


find_real_file.png