Facing issues to run a condition if the Scheduled Job

p_mriganka
Tera Contributor

Hi Team, I am facing one issue to find a logic in Scheduled Job. 

The requirement is to only RUN the IF condition, if it is a weekday (Mon-Fri) or there is a holiday on any weekday. Eg- Jan 01 was on Wednesday, so the IF condition should not run.

But after multiple tries, I couldn't get the solution, so will be grateful, if anyone can help.

 

Attaching the snippet I tried :-

 

var currentDay = new GlideDateTime().getDayOfWeekLocalTime();
var currentDate = new GlideDateTime().getLocalDate().toString(); // Get the current date
var currentMonthDay = currentDate.substring(5); // Extract the MM-DD part of the date
var holidays = ["01-01", "01-26", "08-15", "10-02"]; // List of holidays

// Debugging statements
gs.print("Current Day: " + currentDay);
gs.print("Current Date: " + currentDate);
gs.print("Current Month-Day: " + currentMonthDay);
gs.print("Is Holiday: " + holidays.includes(currentMonthDay));

// Check if the current day is a weekday (Monday to Friday) and not a holiday
if ((currentDay >= 1 && currentDay <= 5) && holidays.includes(currentMonthDay)) {
    gs.print(currentMonthDay); //till weekday it is working fine, but it is running on holidays, even if the holiday is on a weekday
}
1 REPLY 1

Viraj Hudlikar
Tera Sage

Hello @p_mriganka 

Some changes to your code will make it work.

  1. Logic Inversion: Changed holidays.includes() to !holidays.includes() (only run on weekdays that are NOT holidays)

  2. Day of Week Numbers:

    • Monday = 1

    • Friday = 5

    • Saturday = 6

    • Sunday = 0

Refined code now:

 

var currentDay = new GlideDateTime().getDayOfWeekLocalTime(); // 1=Mon, 5=Fri, 6=Sat, 0=Sun
var currentDate = new GlideDateTime().getLocalDate().toString();
var currentMonthDay = currentDate.substring(5); // Get MM-DD format
var holidays = ["01-01", "01-26", "08-15", "10-02"];

// Check if it's a weekday AND NOT a holiday
if (currentDay >= 1 && currentDay <= 5 && !holidays.includes(currentMonthDay)) {
    // YOUR CODE TO EXECUTE ON WORKING WEEKDAYS
    gs.print("Running business logic for " + currentDate);
} else {
    gs.print("Skipping execution - Weekend or holiday");
}

 


If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.