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

Rohith Sabbinen
Mega Expert

Hi Ursnani,



please try below script,



var schedRec = new GlideRecord('cmn_schedule');


schedRec.get('name', '8-5 weekdays');


if (typeof GlideSchedule != 'undefined')


    var sched = new GlideSchedule(schedRec.sys_id);


else


    var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);




//Get the current date/time in correct format for duration calculation


var currentDateTime = current.opened_at.getGlideObject();




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


var timeToAdd = 8 * 32400;


durToAdd = new GlideDuration(timeToAdd*1000);


var newDateTime = sched.add(currentDateTime, durToAdd, '');




//Set the 'due_date' field to the new date/time


current.due_date = newDateTime;



Thanks,


Rohith.


ccajohnson
Kilo Sage

I have a SubScript that I typically use to get the endDateTime through the Duration Calculator. Since we already have the Start Date, the expected duration in seconds, and the sys_id of the Schedule we can run the function to get the datetime. I have modified your script include to fold this code in:


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;


              var startingDateTime = new GlideDateTime();


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


              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'



});


I had to adjust your timeToAdd since it was calculating milliseconds instead of seconds.


Hi,


Thanks for the Code adjustment, but its still calculating Calendar days but not the Business days


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'



});