Duedate Should be Business days

ursnani
Giga Guru

Hi,

I have a requirement as follows,

based on the priority the Duedate should be populated, i was able to get the due date but its taking the calender days, but not the Business days. can anyone please suggest with this, below is the script include for calculating 3 business days but it takes 3 calendar days instead.

var DueDate = Class.create();

DueDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

     

      threedays: function(){

     

      var scheduleName = '8-5 weekdays excluding holidays';

      var days = 3;

      var schedRec = new GlideRecord('cmn_schedule');

        schedRec.get('name', scheduleName);

        var sched = new GlideSchedule(schedRec.sys_id);

  //Set the amount of time to add (in seconds)

  var timeToAdd = days*9*60*60*1000;

  var durToAdd = new GlideDuration(timeToAdd);

  var startingDateTime = new GlideDateTime();

  // run the addition

  var todayDateTime = new GlideDateTime();

  todayDateTime = todayDateTime.getDate();

  // Unfortunately GlideSchedule doesn't do subtraction. Start by subtracting 2 days from today

  var newDateTime = new GlideDateTime();

    newDateTime.addDays(days);

  var newDateTimeStr = newDateTime.getDate();

  return newDateTimeStr;

  },

  type: 'DueDate'

});

Can anyone please help me with this.      

1 ACCEPTED SOLUTION

When I ran your function as a background script, it gave me a warning and used no schedule at all. Rather than try to look up the Schedule to get the sys_id, we can just use the sys_id. In my demo instance the 8-5 weekdays excluding holdiays uses 090eecae0a0a0b260077e1dfa71da828 so I adjusted to suit:


var DueDate = Class.create();


DueDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {


   


      threedays: function() {


              var schedID = '090eecae0a0a0b260077e1dfa71da828'//8-5 weekdays excluding holidays;


              var days = 3;



              //Set the amount of time to add (in seconds)


              var timeToAdd = days*9*60*60;


              var startingDateTime = new GlideDateTime();


              var newDateTimeStr = this._getNextBusDateTime(startingDateTime, timeToAdd, schedID);


              return newDateTimeStr;


      },



      _getNextBusDateTime: function(dateTime, durSec, schedID) {


              var dc = new DurationCalculator();


              dc.setSchedule(schedID, gs.getSession().getTimeZone());


              var busHours = 9;


              var sdt = new GlideDateTime(dateTime);


              dc.setStartDateTime(sdt);


              var cDuration = durSec;


              if (durSec >= 86400) {


                      var busDays = durSec / (60 * 60 * 24);


                      cDuration = busDays * busHours * 3600;


              }


              if (!dc.calcDuration(cDuration)) {


                      gs.log("*** Error calculating duration");


                      return;


              }


              return dc.getEndDateTime();


      },



      type: 'DueDate'



});


View solution in original post

13 REPLIES 13

Also, if you ever want to test your code outside of the Script Include, you can do so using Scripts - Background. I typically adjust the Script syntax so that the functions run as functions rather than an extention of the include. If you compare with what I just posted, you can see the difference.


var newDate = threedays();


gs.print(newDate);



function threedays() {


//       threedays: function() {


              var schedID = '090eecae0a0a0b260077e1dfa71da828'//8-5 weekdays excluding holidays;


              var days = 3;



              //Set the amount of time to add (in seconds)


              var timeToAdd = days*9*60*60;


              var startingDateTime = new GlideDateTime();


              var newDateTimeStr = _getNextBusDateTime(startingDateTime, timeToAdd, sched);


//               var newDateTimeStr = this._getNextBusDateTime(startingDateTime, timeToAdd, sched);


              return newDateTimeStr;


//       },


}



function _getNextBusDateTime(dateTime, durSec, schedID) {


//       _getNextBusDateTime: function(dateTime, durSec, schedID) {


              var dc = new DurationCalculator();


              dc.setSchedule(schedID, gs.getSession().getTimeZone());


              var busHours = 9;


              var sdt = new GlideDateTime(dateTime);


              dc.setStartDateTime(sdt);


              var cDuration = durSec;


              if (durSec >= 86400) {


                      var busDays = durSec / (60 * 60 * 24);


                      cDuration = busDays * busHours * 3600;


              }


              if (!dc.calcDuration(cDuration)) {


                      gs.log("*** Error calculating duration");


                      return;


              }


              return dc.getEndDateTime();


//       },


}


Let me know if you have any questions on using this technique to test.


Thanks Christopher this has helped me.


i changed the schedule and gave the sys_id and it worked.


Hi,



I am sorry nut again this code is not working for me. its not working as expected and its giving the time and I dont want time,i want only the date.


Please clarify what you mean by only the date. From what I recall the due_date field is a date/time field. Please provide your client side script so we can adjust accordingly.


Due_date is not date/time field its only Date field.



Client Script:



function onChange(control, oldValue, newValue, isLoading) {


      if (isLoading || newValue == '') {


              return;


      }


     


      var gr = g_form.getValue('msr_priority');


     


      if( gr == 'urgent'){


             


              var ga = new GlideAjax('Duedate1');


              ga.addParam('sysparm_name', 'oneday');


              ga.getXML(Process);


      }


      else if(gr == 'high'){


             


              var ab = new GlideAjax('DueDate');


              ab.addParam('sysparm_name', 'threedays');


              ab.getXML(Process);


      }


      else if(gr == 'medium'){


             


              var bc = new GlideAjax('Duedate2');


              bc.addParam('sysparm_name', 'sevendays');


              bc.getXML(Process);


      }


      else {


             


              var ad = new GlideAjax('Duedate3');


              ad.addParam('sysparm_name', 'fourteendays');


              ad.getXML(Process);


      }


      function Process(response){


             


              var answer = response.responseXML.documentElement.getAttribute("answer");


              g_form.setValue('msr_due_date',answer);


              g_form.setReadOnly('msr_due_date', true);


      }


}



Script Include:



I am using the Same code you have given its working good for one business days but not for others, which is 3 days, 7 days and 14 days.