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.

Regarding glideDateTime

ShubhangiA77524
Tera Contributor

I have a piece of code and in that script I want RITMs to autocreate on every 8th dau of month, I want it to run on 8th day of every month and if 8th day is a weekend it should skip and then run on Monday i.e. should only run on weekdays.But when I am executing it is not running as desired.

Can anyone help me with a workaround?

date script:


    /*var today = new GlideDateTime();
    var gDate = new GlideDate();
    gDate.setValue(today.getDate());
    var dayOfMonth = parseInt(gDate.getDayOfMonth(), 10);
    var dayOfWeek = parseInt(gDate.getDayOfWeek(), 10); // 1=Sunday, 7=Saturday
    if (dayOfMonth !== 7) {
        gs.info("Today is not the 7th of the month");
        return;
    }
    if (dayOfWeek === 7 || dayOfWeek === 1) {
        gs.info(" 7th falls on a weekend");
        return;
    }
    gs.info(" It's the 7th and a weekday");*/
8 REPLIES 8

Nawal Singh
Tera Guru

Hi @ShubhangiA77524 ,

Please review below code - 

 

// Get current date/time
var now = new GlideDateTime();
var gDate = new GlideDate();
gDate.setValue(now.getDate());

// Get day of month and day of week (1=Sunday, 7=Saturday)
var dayOfMonth = parseInt(gDate.getDayOfMonth(), 10);
var dayOfWeek = parseInt(gDate.getDayOfWeek(), 10);

gs.info("Today: " + gDate.getDisplayValue() + ", Day of month: " + dayOfMonth + ", Day of week: " + dayOfWeek);

// Run logic only on 8th or next Monday if 8th was weekend
var shouldRun = false;

// Case 1: today is 8th and weekday
if (dayOfMonth === 8 && dayOfWeek >= 2 && dayOfWeek <= 6) {
    shouldRun = true;
}
// Case 2: today is 9th (Monday) and the 8th was Sunday
else if (dayOfMonth === 9 && dayOfWeek === 2) { // Monday = 2
    var prevDate = new GlideDate();
    prevDate.setValue(gDate);
    prevDate.addDaysUTC(-1);
    var prevDayOfWeek = parseInt(prevDate.getDayOfWeek(), 10);
    if (prevDayOfWeek === 1) shouldRun = true; // 8th was Sunday
}
// Case 3: today is 10th (Monday) and the 8th was Saturday
else if (dayOfMonth === 10 && dayOfWeek === 2) {
    var prev2 = new GlideDate();
    prev2.setValue(gDate);
    prev2.addDaysUTC(-2);
    var prev2DayOfWeek = parseInt(prev2.getDayOfWeek(), 10);
    if (prev2DayOfWeek === 7) shouldRun = true; // 8th was Saturday
}

if (shouldRun) {
    gs.info(" Running RITM creation logic today.");
    // ==== Your RITM creation logic here ====
} else {
    gs.info("⏸ Not the correct day to run the job.");
}

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh

I runned it in background script and it showed following log:

*** Script: Today: 11/07/2025, Day of month: 6, Day of week: 5
*** Script: Not the correct day to run the job.

Hi @ShubhangiA77524 ,

 

add the date that you want to create a RITM and test it.

so it will confirm that it will work.

 

If you found my response helpful, please mark it as helpful and accept it as the solution.

Thank you
Nawal Singh

I used the day 7 in this script as today is 7 but it did not worked, can you help me with it