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;


}



},


View solution in original post

32 REPLIES 32

Yes. It is new


patricklatella
Mega Sage

ahh, this was the log I got back (I changed the ++++ to make the logs line up in a filtered view)



find_real_file.png


Thanks. One more time please. Sorry I wish I could access your instance and do it myself.


Also confirm this schedule 090eecae0a0a0b260077e1dfa71da828 is 8 * 5 Weekday schedule.



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,""));



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


gs.log('++++++++++++++++++hours to compare   ++++++++++'+ pn);



if(dif >= pn){


retVal = "True";


} else {


retVal = "False";


}


gs.log(retVal);


return retVal;


}



},


patricklatella
Mega Sage

yes, that sys_id is correct.   here is the log result...



find_real_file.png


doesn't appear to be getting to the last 3 gs.logs in the script


patricklatella
Mega Sage

I think something is up with this line:



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