Schedule Job for Selected day of Selected Month

Community Alums
Not applicable

Hi All,

I have a requirement for scheduled jobs to run on the on a certain day on certain months, for example -

Run on 1st of March, June, September and December.

Is there a conditional script to achieve this and to miss out weekends?

Many thanks,

Alex

 

1 ACCEPTED SOLUTION

Michael Fry1
Kilo Patron

Set your scheduled job to run Monthly, on the date you need and time. Then in the condition script check for the month to trigger. Example below script triggers on 1 month, 4 month, etc

find_real_file.png

 

 

var result = false;
var gdt = new GlideDateTime();
if (gdt.getMonthLocalTime() == 1 || gdt.getMonthLocalTime() == 4 || gdt.getMonthLocalTime() == 7 || gdt.getMonthLocalTime() == 10) {
	result = true;
}
result;

View solution in original post

10 REPLIES 10

Ujjawal Vishnoi
Mega Sage
Mega Sage

Hi Alex,

 

As per my understanding you want to run job on 2nd if 1st is weekend?

 

For this case you need to create run your job daily and check day and month and weekend in your script.

 

Regards,

Ujjawal

Community Alums
Not applicable

Hi Ujjawal,

 

I would like the same job to run on the 1st of March, the 1st of June, the 1st of September and the 1st of December and if any of them days falls on a weekend to schedule it for the Monday instead.

 

Can you advise on the script that would achieve this?

 

Thanks!

Alex

 

 

Hi Alex,

So you want to run job only on 1st of every month; but if that is weekend i.e. saturday or sunday then it should run for monday instead for that month?

Through script you can know when to run the job; but the issue would be if 1st of that month is a weekend then on coming monday you need to execute that month's job; you would require to keep track of the job which ran on 1st and job which could have run on 1st but didn't run since it was a weekend

since your job is running daily; it would pick the job whether it got executed or not on 1st of that month; if not then execute now since it is a monday

Regards

Ankur

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

ccajohnson
Kilo Sage

You should be able to set your Scheduled Job to run daily, then you should be able to run a condition script that determines the current month, current date, current day. If I am not mistaken, you set a variable answer to be true or false in the condition script. I have put together a script that you can run in Scripts - Background as proof that it works:

answer = chkDate();
gs.info(answer); //this can be removed when in the condition script

function chkDate() {
    var d = new Date();
    var mOfY = d.getMonth(); //Month of the Year
    var isMo = (mOfY == 2 || mOfY == 5 || mOfY == 8 || mOfY == 11); //Is Mar, Jun, Sep, Dec
    var dOfM = d.getDate(); //Day of the Month
    var dOfW = d.getDay(); //Day of the week
    var dInW = (dOfW > 0 && dOfW < 6); //Is a weekday
    var retVal = false;
    //When the 1st falls on a weekday
    if (isMo && dOfM == 0 && dInW) {
        retVal = true;
    }
    //When the 2nd falls on a Monday
    if (isMo && dOfM == 1 && dOfW == 1) {
        retVal = true;
    }
    //When the 3rd falls on a Monday
    if (isMo && dOfM == 2 && dOfW == 1) {
        retVal = true;
    }
    return retVal;
}

When testing in Scripts - Background, feel free to adjust the value of dOfW to be 0, 1, or 2 and you should have the answer to be true. When you are satisfied, you can remove the gs.info portion of the above script and add to your condition on the Scheduled Job.

If you have any questions, let us know.