The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Scheduled Job to run quarterly first month starting second Monday to 10 business days

Bhaskar24
Tera Expert

Hello Developers!

 

I am trying to configure a scheduled job to run starting 2nd Monday until 10 business days in first month of the quarter.
Ex: In Q4 2025, I need to run on Oct 13 to 17 and 20 to 24.

 

Can anyone help me on that please?

 

Thanks in advance!

1 ACCEPTED SOLUTION

Bhaskar24
Tera Expert

I created a Scheduled Job with the below script in the 'Condition'.

 

var answer = false;
var today = new GlideDateTime(); // Today's date
var month = today.getMonthUTC();
var firstDayOfMonth = new GlideDateTime(today.getYearUTC() + '-' + month + '-01 00:00:00');
var maxBusinessDays = 10;
var secondMonday;
var businessDaysPassed;

if (month == 1 || month == 4 || month == 7 || month == 10) {
    if (today.getDayOfWeekUTC() == 6 || today.getDayOfWeekUTC() == 7 || today.getDayOfMonthUTC() <= 7 || today.getDayOfMonthUTC() >= 26) { // Check if Saturday or Sunday or the date is below 8 or above 26
        answer = false;
    } else {
        secondMonday = secondMondayInMonth(firstDayOfMonth);
        businessDaysPassed = businessDays(firstDayOfMonth, today);
        if (businessDaysPassed >= 1 && businessDaysPassed <= maxBusinessDays && secondMonday.compareTo(today) <= 0) { // Check if business days are from 1 to max and the date is after second Monday
            answer = true;
        } else {
            answer = false;
        }
    }
}
answer;

// Function to get second Monday of first Month in the Quarter
function secondMondayInMonth(startDate) {
    var days = 0;
    while (days == 0) {
        if (startDate.getDayOfMonthUTC() >= 8 && startDate.getDayOfMonthUTC() <= 14 && startDate.getDayOfWeekUTC() == 1) { // Check if the date is from 8 to 14 and Monday
            days++;
            return startDate;
        }
        startDate.addDaysUTC(1);
    }
}

// Function to calculate business days between two dates
function businessDays(startDate, endDate) {
    var date = new GlideDateTime(startDate);
    var busDays = 0;
    var dayOfWeek;
    while (date.compareTo(endDate) <= 0) {
        dayOfWeek = date.getDayOfWeekUTC();
        if (dayOfWeek >= 1 && dayOfWeek <= 5) { // Monday to Friday are business days
            busDays++;
        }
        date.addDaysUTC(1);
    }
    return busDays;
}

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Bhaskar24 

I believe you can use daily scheduled job and check this

1) if it's 2nd Monday of 1st month of the current quarter

2) also you need to check in the same script by adding business days to the 2nd monday and only allow it to run when the business days don't cross 10 count

see these links for help

Add Business Days 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you for the response, Ankur!

Bhaskar24
Tera Expert

I created a Scheduled Job with the below script in the 'Condition'.

 

var answer = false;
var today = new GlideDateTime(); // Today's date
var month = today.getMonthUTC();
var firstDayOfMonth = new GlideDateTime(today.getYearUTC() + '-' + month + '-01 00:00:00');
var maxBusinessDays = 10;
var secondMonday;
var businessDaysPassed;

if (month == 1 || month == 4 || month == 7 || month == 10) {
    if (today.getDayOfWeekUTC() == 6 || today.getDayOfWeekUTC() == 7 || today.getDayOfMonthUTC() <= 7 || today.getDayOfMonthUTC() >= 26) { // Check if Saturday or Sunday or the date is below 8 or above 26
        answer = false;
    } else {
        secondMonday = secondMondayInMonth(firstDayOfMonth);
        businessDaysPassed = businessDays(firstDayOfMonth, today);
        if (businessDaysPassed >= 1 && businessDaysPassed <= maxBusinessDays && secondMonday.compareTo(today) <= 0) { // Check if business days are from 1 to max and the date is after second Monday
            answer = true;
        } else {
            answer = false;
        }
    }
}
answer;

// Function to get second Monday of first Month in the Quarter
function secondMondayInMonth(startDate) {
    var days = 0;
    while (days == 0) {
        if (startDate.getDayOfMonthUTC() >= 8 && startDate.getDayOfMonthUTC() <= 14 && startDate.getDayOfWeekUTC() == 1) { // Check if the date is from 8 to 14 and Monday
            days++;
            return startDate;
        }
        startDate.addDaysUTC(1);
    }
}

// Function to calculate business days between two dates
function businessDays(startDate, endDate) {
    var date = new GlideDateTime(startDate);
    var busDays = 0;
    var dayOfWeek;
    while (date.compareTo(endDate) <= 0) {
        dayOfWeek = date.getDayOfWeekUTC();
        if (dayOfWeek >= 1 && dayOfWeek <= 5) { // Monday to Friday are business days
            busDays++;
        }
        date.addDaysUTC(1);
    }
    return busDays;
}