How to Condition Schedule Job to run on certain weeks and month

Joshua Comeau
Kilo Sage

I have a weekly, monthly and quarterly, semi, or annual scheduled jobs.

 

Looking to fix the code as it is currently not working / looking for advice

 

Condition:

Weekly task needs to run every week except the first week of a month, except 2 or 4 months of the year we want the weekly to run in the first week of the month, as there is no monthly to take its place. Rather than monthly tasks in those 2 or 4 months, we would have a quarterly, semi, or annual.

 

Current Conditional Code not working:

//start

function checkMonth()

 

{

var gd = new GlideDateTime();

var getWeek = gd.getWeek();

var getMonth = gd.getMonth();

//Update weeks as per your requirement

if(getWeek == 1 && getMonth == 2 || getMonth == 8 ){

return true;

}

if(getWeek == 1 && getMonth != 2 || getMonth != 8 ){

return false;

}

else{

return true;

}}

//end

2 REPLIES 2

Mark Manders
Mega Patron

Sorry, I don't have a solution, because I find your question very confusing and have no idea when you need to run them (and why you don't just run them always), but your conditions have a flaw (I tink at least, if that's where they are going wrong).

You are (stating getWeek == 1 && getMonth != 2 || getMonth !=8). This will always return true, because it's either not month 2 or not month 8. 
Example: month 1 is true (not month 2 and not month 8. Month 2 is true, because it's not month 8, month 8 is true because it's not month 2.

As I said: I am confused on what you are asking for, because also your first one is weird to me: you check on week 1 from a glidedatetime. Did you get the result through logging? How can week one ever be in month 2? Shouldn't that be week = 1 || month = 2? 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

I want if it is the first week of the month and it is a specific month example 2=FEB or 8=AUG do not run the scheduled job else run

 

this is a weekly scheduled jobs that is colliding with another monthly semi and quarterly job

 

new code 

//start

function checkMonth()

{

var gd = new GlideDateTime();

var getWeek = gd.getWeek();

var getMonth = gd.getMonth();

 

//Update weeks as per your requirement

 

if(getWeek == 1 && getMonth == 2 && getMonth == 8) {

return true;

}

 

else{

return false;

}}

}

//end