Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

Ankur Bawiskar
Tera Patron

@ShubhangiA77524 

make your job run daily and it will handle this

(function() {
    var gdt = new GlideDateTime();
    var gDate = new GlideDate();
    gDate.setValue(gdt.getDate());
    var dayOfMonth = gDate.getDayOfMonth();
    var dayOfWeek = gdt.getDayOfWeek(); // 1=Monday ... 7=Sunday

    // Run only on weekdays (Monday=1 to Friday=5)
    var isWeekday = (dayOfWeek >= 1 && dayOfWeek <= 5);

    // If today is 8th and weekday, run
    if (dayOfMonth === 8 && isWeekday) {
        gs.info('Running on 8th which is a weekday');
        // Call your RITM auto-creation logic here
        return;
    }

    // If today is Monday, check if 8th was on weekend (Sat or Sun)
    if (dayOfWeek === 1) { // Monday
        var gdtYesterday = new GlideDateTime();
        gdtYesterday.addDaysLocalTime(-1);
        var dayOfMonthYesterday = gdtYesterday.getDayOfMonth();

        // If yesterday was 8th and weekend, run today (Monday)
        if ((dayOfMonthYesterday === 8) && (gdtYesterday.getDayOfWeek() >= 6)) {
            gs.info('8th was weekend, running on Monday');
            // Call your RITM auto-creation logic here
            return;
        }
    }

    gs.info('Not the scheduled day for running RITM creation');
})();

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

i customized this with my day like 7 but it did not worked 

@ShubhangiA77524 

what debugging did you do?

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

instead of 8 day i edited it with 7 and run it as background script , then in schedule job , but schedule job did not run and no records were created