Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Choose only first business day of the next months

SCruz
Mega Contributor

Hi, guys.

 

I need to be able to choose just the first business day of any month ahead.

I have a Schedule of working days and I need to use it, but I don't know what can I do to be able to choose only these first working days of each next month. Could someone help me?

 

thanks!

3 REPLIES 3

Community Alums
Not applicable

@SCruz Identify the start of the upcoming month (e.g., 01-December-2024).

Use the GlideSchedule API to iterate from the first day of the month and find the first valid working day.

Demo Script you can try if helps you:

function getFirstBusinessDayOfMonth(scheduleName, year, month) {

    // Create a GlideDateTime object for the first day of the specified month

    var firstDay = new GlideDateTime();  firstDay.setDisplayValue(`01-${month}-${year}`);

    // Create a GlideSchedule object for the specified work schedule

    var schedule = new GlideSchedule(scheduleName, 'sys_id', '123'); // Replace with your schedule's sys_id

    // Iterate through days until a working day is found

    while (!schedule.isInSchedule(firstDay)) {

        firstDay.addDaysLocalTime(1);

    }

    // Return the first business day as a display value

    return firstDay.getDisplayValue();

}

// Example:

var firstBusinessDay = getFirstBusinessDayOfMonth("Company Work Schedule", 2024, "12");

gs.info("The first business day of December 2024 is: " + firstBusinessDay);

Animesh Das2
Mega Sage
Mega Sage

Hi @SCruz ,

 

You can create a new business calendar (for example, Working month) navigating Business calendar> Business calendars. Then create all 12 months entries under 'Business calendar entries' related list for upcoming years with start (as per working start date) and end of each month date. Refer to this 1st SS below.

ServiceNow document reference: Creating business calendars 

AnimeshDas2_0-1731697228509.png

Then select the 'Run' and 'Business calendar' field values in the scheduled job you created as shown in below SS.

AnimeshDas2_1-1731697481500.png

 

If this address your question, please don't forget to mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

Thanks, 

Animesh Das

 

nikhilmadhe
Tera Contributor

Please try this below code to get the 1st business day of the specific month.

function getFirstBusinessDayOfMonth(year, month, time) {
    // Create a GlideDateTime object for the first day of the specified month.
    var firstDay = new GlideDateTime();

    var date_time_format=gs.getDateTimeFormat();
    date_time_format=date_time_format.replace('yyyy',year);
    date_time_format=date_time_format.replace('MM',month);
    date_time_format=date_time_format.replace('dd','01');
    date_time_format=date_time_format.replace('HH:mm:ss',time);

    firstDay.setDisplayValue(date_time_format);

    // Create a GlideSchedule object for the specified work schedule.
    // Replace the actual sys_id of your schedule.
    var schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae');


    // Iterate through days until a working day is found.
    while (!schedule.isInSchedule(firstDay)) {
        firstDay.addDaysLocalTime(1);
    }

    // Return the first business day as a display value.
    return firstDay.getDisplayValue();
}

// Example usage:
var firstBusinessDay = getFirstBusinessDayOfMonth('2025', "11", '08:00:00');
gs.info("The first business day of November 2025 is: " + firstBusinessDay);

OUTPUT
function getFirstBusinessDayOfMonth(year, month, time) {
    // Create a GlideDateTime object for the first day of the specified month.
    var firstDay = new GlideDateTime();

    var date_time_format=gs.getDateTimeFormat();
    date_time_format=date_time_format.replace('yyyy',year);
    date_time_format=date_time_format.replace('MM',month);
    date_time_format=date_time_format.replace('dd','01');
    date_time_format=date_time_format.replace('HH:mm:ss',time);

    firstDay.setDisplayValue(date_time_format);

    // Create a GlideSchedule object for the specified work schedule.
    // Replace the actual sys_id of your schedule.
    var schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae');


    // Iterate through days until a working day is found.
    while (!schedule.isInSchedule(firstDay)) {
        firstDay.addDaysLocalTime(1);
    }

    // Return the first business day as a display value.
    return firstDay.getDisplayValue();
}

// Example usage:
var firstBusinessDay = getFirstBusinessDayOfMonth('2025', "11", '08:00:00');
gs.info("The first business day of November 2025 is: " + firstBusinessDay);