I need to calculate a change request lead time by hours, not days. Also call a schedule so I exclude weekends/holidays

Jeff316
Kilo Guru

Hi All,

I need to throw an error on the change request for if the user enters a Planned Start Date that is less than 72 "business hours" from the create date/time.

Today I have a client script (onChange) that calls a script include that references a schedule that excludes weekends and holidays. but its based on days and not hours.

Request: If I create a change request on monday at 4pm then I cannot enter a planned start date less than 72 hours from 4pm Monday. I can enter a planned start date after 4pm on Thursday.

Hoping someone can share their script method to achieve this.

 

 

4 REPLIES 4

Uncle Rob
Kilo Patron

Thank you sir.

Will this actually go to the minute? I now need the lead time to be 4,320 minutes so if I enter a change at monday 4:13pm i cannot enter a planned start date until after 4:13pm on Thursday.

I have a few things to try now in my dev instance to see if I can get the duration in minutes while using my schedule to exclude holidays/weekends.

Tell you the truth, I don't really know.  But you can use a background script or Xplore to test it really fast.

Jeff316
Kilo Guru

After 2 days, I'm still stuck.

I had it working but realized it was working because my date/time preference was 24 hour but if I change my preference to 12 hour time format, it doesn't work because my script needs to look at the "hour". Well in 24 hour format the hour would be 15 so I can add 15 but in 12 hour format the hour is 3, so my method is off. So I started playing with methods that look at the users format but haven't figured it out yet. 

It gets complicated because I have to run the date/time against a schedule first to exclude holidays and weekends. Then I need to get the diff between start and end. I would like to just get the diff in minutes or seconds but my method has to be able to get the diff between 2 date/times regardless of the date/time format the user has selected for their GUI. 

Here is one method that appeared to work if the user format was 24 hour time.

I use an onChange client script to send the change request's Create Date/Time and Planned Start Date/Time as parameters to this script include.

var AjaxChangeHelper2 = Class.create();
AjaxChangeHelper2.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    //return the difference between two dates in days
    getDaysDiff: function() {
        var date1 = this.getParameter('sysparm_date1');
        var date2 = this.getParameter('sysparm_date2');

        var schedule = new GlideSchedule();
        schedule.load("6c9a79601b2a84100560a9bfbd4bcbd2"); // 24 hours day - weekdays excluding holidays

        var dur = schedule.duration(new GlideDateTime(date1), new GlideDateTime(date2));

        var days = dur.getDayPart();
        var hours = dur.getByFormat("HH");
        var minutes = dur.getByFormat("mm");

        var businessHours = (days * 24 + hours * 1);
        var businessMinutes = (businessHours * 60);
		var durMins = (businessMinutes + minutes*1);
		gs.log("dur is "+dur);
        gs.log("days are " + days);
        gs.log("hours are " + hours);
        gs.log("minutes are " + minutes);
        gs.log("businessHours are " + businessHours);
		gs.log("businessMinutes are "+businessMinutes);
        return durMins;
    },

    hasOutage: function() {
        var outage = new GlideRecord('cmdb_ci_outage');
        outage.addQuery('task_number', this.getParameter('sysparm_sys_id'));
        outage.query();
        if (outage.hasNext()) {
            return true;
        } else {
            return false;
        }
    },

    type: 'AjaxChangeHelper2'
});