logo

NJP

How to go about sending a notification on the First, Second, Third business day and 7th business days before the end of the month.

Import · Jan 26, 2021 · article

So I had a requirement to send 4 notifications. These notifications need to be sent on the first, second and third business day of the month. The last one needs to be sent 7 days before the end of the month. Usually this is fairly easy but what throws a wrench into the process is that the notifications are sent based on Business days, so weekends and company holidays need to be excluded. So there are a few ways you can do this. The first is to use durations and have the Schedule object to find the correct end date using a duration. The issue with this is you have to know how many hours are in each day for the schedule you are using. Meaning if your schedule is 9 to 5 that is an 8 hour day and so you have to do your calculations based on that. The other issue is that you cannot as far as I am ware have a negative duration. So I choose to do what I feel is simpler and just add one day to the date and check to see if its in the schedule. This way I do not have to deal with the numbers of hours per day I just have to make sure that the job runs during the business hours that the schedule covers. So if my schedule is 9 to 5 then I need to schedule it during that window. If you use 24 hour days you can schedule the job to run any time.

I'm sure someone will find a more efficient way to go about it or make it a generic function which I will do at some point but felt people would find the code helpful.

(function(){
    var dt = new GlideDateTime();
    var day = dt.getDayOfMonthUTC();
    var sched = new GlideSchedule("79ff5d90db8af2800b865dd5ce9619f9"); //24x7 Except Weekends and Holidays

    //Get the first day of the month.
    var date = new GlideDateTime();
    date.setDayOfMonthLocalTime(1);

    //Get the last day of the month.
    var sevenDate = new GlideDateTime();
    sevenDate.setDayOfMonthLocalTime(sevenDate.getDaysInMonthLocalTime());
    var sevenDayCount = 0;
    var loopCount = 0; //For preventing infinit loop
    var dates = {};
    do {
        if(!dates.one && sched.isInSchedule(date)){
            dates.one = new GlideDateTime(date);
            date.addDaysLocalTime(1);
        }
        if(dates.one && !dates.two && sched.isInSchedule(date)){
            dates.two = new GlideDateTime(date);
            date.addDaysLocalTime(1);
        }
        if(dates.two && !dates.three && sched.isInSchedule(date)){
            dates.three = new GlideDateTime(date);
            date.addDaysLocalTime(1);
        }
        date.addDaysLocalTime(1);

        if(!dates.seven && sched.isInSchedule(sevenDate)){
            //Count the business days and when we get to the one we want save it.
            //Remember that we need 7 business days from the end of the month.
            sevenDayCount += 1;
            if(sevenDayCount == 7){
                dates.seven = new GlideDateTime(sevenDate);
            }
            sevenDate.addDaysLocalTime(-1);
        } else
            sevenDate.addDaysLocalTime(-1);

        loopCount += 1;
    } while((!dates.one || !dates.two || !dates.three || !dates.seven) && loopCount < 31); //

    //Get today so we can check to see if an event needs to be triggered.
    date = new GlideDateTime();
    if(dates.one.getDayOfMonthLocalTime() == date.getDayOfMonthLocalTime()){
        //Day 1 notification
        gs.eventQueue("reminder_" + day, u, u.getValue("sys_id"));
    }
    if(dates.two.getDayOfMonthLocalTime() == date.getDayOfMonthLocalTime() && dates.three.getDayOfMonthLocalTime() == date.getDayOfMonthLocalTime()){
        //Day 2, 3 notification
        gs.eventQueue("reminder_" + day, u, u.getValue("sys_id"));
    }
    if(dates.seven.getDayOfMonthLocalTime() == date.getDayOfMonthLocalTime()){
        //7 Days before 1st of Month needs to be checked.
        gs.eventQueue("reminder_7", u, u.getValue("sys_id"));
    }

})();
View original source

https://www.servicenow.com/community/developer-articles/how-to-go-about-sending-a-notification-on-the-first-second-third/ta-p/2310408