Due Date based on schedule and duration from SLA

Brian Lancaster
Tera Sage

I trying to set the due date based on the schedule and duration from the SLA on the RITM. I'm doing via a script because we have a log of generic workflow that just create 1 task and there are several different SLAs. Also just like the SLA is setup to start only after the RITM has been approved. The problem I am facing is that it is not following the schedule fully. My schedule is Monday - Friday from 8 - 5. I have an SLA setup is a 9 hour duration or 1 business day. When my script run it is setting the end time at 7 am the next day but 7 am is not in the schedule. Is there something I'm doing wrong. Here is my script include I'm passing the sys_id of the SLA that is on that catalog item.

var setDueDate = Class.create();
setDueDate.prototype = {
    initialize: function() {},
    CalculateDueDate: function(SLA) {
        var gr = new GlideRecord('contract_sla');
	var dur = "";
        gr.addQuery('sys_id', SLA);
        gr.query();
        if (gr.next()) {
            var temp = gr.duration.getDisplayValue().toString().split(' ');
			if (temp.indexOf('Days') > -1 || temp.indexOf('Day') > -1){
				dur = temp[0] + " " + temp[2] + ":00:00";
			}
			else {
				dur = temp[0] + ":00:00";
			}
            var startDate = new GlideDateTime(gs.notDateTime());
            var fullDur = new GlideDuration(dur);
            var schedule = new GlideSchedule(gr.schedule, 'US/Pacific');
            var end = schedule.add(startDate, fullDur);
            return end;
        }
    },

    type: 'setDueDate'
};
1 ACCEPTED SOLUTION

DrewW
Mega Sage
Mega Sage

Ok so I ran this as a background script using one of our SLA's that is three days 8 to 5 except weekends and as long as I left out the time zone when setting up the schedule var it works correctly.  So I think you need to do the fix above and then remove the time zone.  Once you set the due date it will automatically get displayed in the users time zone and I think thats whats throwing it off.  There are two time zone offsets being included in the code you started with.

var setDueDate = Class.create();
setDueDate.prototype = {
    initialize: function() {},
    type: 'setDueDate'
};

setDueDate.CalculateDueDate = function(SLA) {
    var gr = new GlideRecord('contract_sla');
    if (gr.get(SLA)) {
        var startDate = new GlideDateTime();

gs.print(startDate.getDisplayValue());

        var fullDur = gr.duration.getGlideObject()
        var schedule = new GlideSchedule(gr.schedule);
        var end = schedule.add(startDate, fullDur);
        return end;
    }
}

gs.print((setDueDate.CalculateDueDate("270ef00cd7133200bbc783e80e6103f1")).getDisplayValue());

View solution in original post

13 REPLIES 13

DrewW
Mega Sage
Mega Sage

So another thing I noticed is you did

var startDate = new GlideDateTime(gs.nowDateTime());
 
This returns the current date/time in the users format, i.e. its a display value.  So you need to just do 
var startDate = new GlideDateTime();
 
To get a date/time that is right now.  Thats probably not going to fix the issue but it could throw things off.

DrewW
Mega Sage
Mega Sage

Ok so I ran this as a background script using one of our SLA's that is three days 8 to 5 except weekends and as long as I left out the time zone when setting up the schedule var it works correctly.  So I think you need to do the fix above and then remove the time zone.  Once you set the due date it will automatically get displayed in the users time zone and I think thats whats throwing it off.  There are two time zone offsets being included in the code you started with.

var setDueDate = Class.create();
setDueDate.prototype = {
    initialize: function() {},
    type: 'setDueDate'
};

setDueDate.CalculateDueDate = function(SLA) {
    var gr = new GlideRecord('contract_sla');
    if (gr.get(SLA)) {
        var startDate = new GlideDateTime();

gs.print(startDate.getDisplayValue());

        var fullDur = gr.duration.getGlideObject()
        var schedule = new GlideSchedule(gr.schedule);
        var end = schedule.add(startDate, fullDur);
        return end;
    }
}

gs.print((setDueDate.CalculateDueDate("270ef00cd7133200bbc783e80e6103f1")).getDisplayValue());

That worked, I'm guessing not using gs.NowDateTime() in the one line was part of the solution too as I tried removed time zone before.

Correct, gs.nowDateTime returns the current date/time in the current users time zone and you need to do things in GMT.  So you where basically accounting for the time zone twice.