Need a scheduled job that starts on Day 1 of the quarter and runs up to the 3rd Wednesday

Bhaskar24
Tera Expert

My requirement is to create a scheduled job that starts on Day 1 of the quarter and runs each business day until the maximum of Wednesday of 13 times up to the 3rd Wednesday of the quarter (whichever comes first).

Can anyone help me in building that condition please? Thanks in advance.

1 REPLY 1

SP22
Mega Sage
Mega Sage
(function executeRule(current, previous /*null when async*/) {
    // Variables
    var today = new GlideDateTime();  // current date
    var firstDayOfQuarter = getFirstDayOfQuarter();  // function to get first day of the quarter
    var thirdWednesday = getThirdWednesdayOfQuarter();  // function to get 3rd Wednesday
    var maxBusinessDays = 13;
    // Function to get the first day of the current quarter
    function getFirstDayOfQuarter() {
        var now = new GlideDateTime();
        var month = now.getMonthUTC() + 1;  // getMonthUTC is zero-based, so we add 1
        var firstMonthOfQuarter = Math.floor((month - 1) / 3) * 3 + 1;
        return new GlideDateTime(now.getYearUTC(), firstMonthOfQuarter, 1, 0, 0, 0);  // First day of the quarter
    }
    // Function to get the 3rd Wednesday of the quarter
    function getThirdWednesdayOfQuarter() {
        var firstDay = getFirstDayOfQuarter();
        var wednesdayCount = 0;
        var date = new GlideDateTime(firstDay);
        while (wednesdayCount < 3) {
            if (date.getDayOfWeekUTC() == 4) {  // 4 represents Wednesday in GlideDateTime
                wednesdayCount++;
            }
            date.addDaysUTC(1);
        }
        return date;
    }
    // Function to calculate business days between two dates
    function getBusinessDays(start, end) {
        var businessDays = 0;
        var date = new GlideDateTime(start);
        while (date.compareTo(end) <= 0) {
            var dayOfWeek = date.getDayOfWeekUTC();
            if (dayOfWeek >= 1 && dayOfWeek <= 5) {  // Monday to Friday are business days
                businessDays++;
            }
            date.addDaysUTC(1);
        }
        return businessDays;
    }
    // Compare today's date with the calculated third Wednesday and 13 business days from the first day
    var businessDaysPassed = getBusinessDays(firstDayOfQuarter, today);
    if (businessDaysPassed <= maxBusinessDays && today.compareTo(thirdWednesday) <= 0) {
        // Schedule the job or execute task
        gs.log("Scheduled job is running for Day " + businessDaysPassed + " of the quarter.");
    } else {
        gs.log("Stopping scheduled job; either 13 business days or 3rd Wednesday has passed.");
    }
})(current, previous);

can you please try above script and let me know whether it is useful or not.

Thanks
SP.