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

patricklatella
Mega Sage

oh, actually seeing you can see the date entered in the log results


Let me know the log for 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).getDurationValue().split(':')[0].substr(-2);



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;


}



},



Please mark this response as correct or helpful if it assisted you with your question.

patricklatella
Mega Sage

could the issue have anything do with the variable being a "Date" type field, and not "date/time"?


No..that shouldnt be....What happened when you run this script. We just need to debug and correct the script.



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('+++++++++++++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;


}



},



Please mark this response as correct or helpful if it assisted you with your question.

patricklatella
Mega Sage

OK all good.   Is that a new script?   a couple posts back in this thread is the log results from the first script you sent.