- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2017 08:10 AM
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.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2017 09:45 AM
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'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2017 12:35 PM
You have to understand that with schedules and using business days, the calculations occur within the business hours. So for example, if your business day runs from 8 am to 4 pm and you are submitting your request outside of business hours then you will get a return at the beginning of the next day. I have adjusted your script within your script include to have your start time always be at the same time (08:00:01 AM)
Also, since you always want a return as just a date instead of a Date/Time string, I have adjusted the return to only give the date.
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 GlideDate() + ' 08:00:01';
var newDateTimeStr = this._getNextBusDateTime(startingDateTime, timeToAdd, schedID);
var dtArray = newDateTimeStr.toString().split(' ');
var newDate = dtArray[0]
return newDate;
},
_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 (!dc.calcDuration(cDuration)) {
gs.log("*** Error calculating duration");
return;
}
return dc.getEndDateTime();
},
Since it appears that the various includes are using the same function only the number of days changes, you may want to consolidate your code into one script within your script include.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2017 01:02 PM
Thanks Christopher Johnson
It working good now.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2017 11:48 AM
Hi,
It is taking today also as count in days.
if we selected today i,e 07/17 then the due date should be 07/20
and also i am applying the same with oen day, 7 days and 14 days which is not working .

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2017 09:33 AM
Hi Ursani,
You can have a after insert business rule written on task_sla table with below code.
//function onAfter(current, previous) { //Kindly keep the function commented
var gr = new GlideRecord('table_name_that_has_sla_tied');
gr.addQuery('sys_id',current.task);
gr.query();
while(gr.next())
{
gs.log('Task is ',current.task.getDisplayValue());
gs.log('Due date is ',current.planned_end_time);
gr.due_date = current.planned_end_time;
gr.setWorkflow(false);
gr.update();
}
//}
Since, Task sla associated to the record would have scheduled associated too it the above script would work well.
Thanks,
Jaspal Singh
Hit Like or Correct on the impact of response.