Scheduled job should be trigger Every Month first Monday

Mani60
Tera Contributor

Hi,

I have one requirement, in that scheduled job should be trigger  first Monday of every month. I have tried but in that way its not working please find the attach snapshot. could you please suggest me proper way.

1 ACCEPTED SOLUTION

Musab Rasheed
Tera Sage
Tera Sage

Hi @ Manish G ,

Try below.? See the yellow lines in screenshot. Don't forgot to mark my answer as correct if that helps.

Condition Script:

answer = (new GlideDateTime().getDayOfMonthLocalTime() <= 7);

Screenshot:

find_real_file.png

Please hit like and mark my response as correct if that helps
Regards,
Musab

View solution in original post

7 REPLIES 7

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Manish,

Following script will get the first Monday of month.

function getFirstMonday() {
    var gdt = new GlideDateTime();

    var curMonth = gdt.getMonthLocalTime();
    var dow = gdt.getDayOfWeekLocalTime();
    gdt.addDaysLocalTime(-(dow - 1));

    var chkMonth = gdt.getMonthLocalTime();
    if (curMonth != chkMonth) {
        gdt.addDaysLocalTime(7);
    } else {
        var week = Math.floor(gdt.getDayOfMonthLocalTime() / 7);
        gdt.addDaysLocalTime(-(week * 7));
    }
    return gdt.getLocalDate();
}

var today = (new GlideDateTime()).getLocalDate();
var firstMonday = getFirstMonth();
answer = (today == firstMonday);

Can be shorten like below.

function checkFirstMonth() {
    var today= new GlideDateTime();
    var gdt =  new GlideDateTime();

    var curMonth = gdt.getMonthLocalTime();
    var dow = gdt.getDayOfWeekLocalTime();
    gdt.addDaysLocalTime(-(dow - 1));

    var chkMonth = gdt.getMonthLocalTime();
    if (curMonth != chkMonth) {
        gdt.addDaysLocalTime(7);
    } else {
        var week = Math.floor(gdt.getDayOfMonthLocalTime() / 7);
        gdt.addDaysLocalTime(-(week * 7));
    }
    //return gdt.getLocalDate();
    return (today == gdt);
}

answer = getFirstMonth();

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use condition field

Try to use GlideDateTime()

Also check point mentioned by Musab

Regards
Ankur

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