How to run the schedule job for every 2 weeks on saturday

Sethu Varma
Tera Expert

Hi Team,

 

I want to run the schedule job for every 2 weeks on saturday. There is no option like every 2 weeks. 

1 ACCEPTED SOLUTION

OlaN
Giga Sage
Giga Sage

Hi,

G Ponsekar answered correctly that you can have it run every other week with the "Conditional" checkbox checked.

But the script is unnecessarily complicated.

A simple "is it an odd or even week" check will do.

 

Example below.

var gdt = new GlideDateTime();
var weekNumber = gdt.getWeekOfYearUTC();
var oddOrEvenWeek = weekNumber % 2;

if (oddOrEvenWeek == 0){
	// run on even weeks, 2, 4, 6 and so on
	return true;
}

// if you want this to run on odd weeks (1,3,5,7 and so on), change the condition to --> (oddOrEvenWeek == 1)

View solution in original post

5 REPLIES 5

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Sethu Varma 

 

For the time being, you'll need to implement a code-based solution for this. However, this issue is resolved in the Zurich release.

 

https://www.servicenow.com/community/developer-forum/run-schedule-job-every-2-weeks/m-p/2103077

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

G Ponsekar
Mega Guru

Hi @Sethu Varma ,

 

Configure Scheduled job like below

  • Name: Give your job a descriptive name (e.g., "EveryTwoWeeksSaturdayJob").
  • Run: Select "Weekly".
  • Time: Set the desired time for the job to run.
  • Day of week: Select "Saturday".
  • Advanced: Select the "Advanced" checkbox.
  • Condition: Add a script to check if the current date is the correct Saturday. This script will filter for every other Saturday.

Script in Condition like:

(function() {
var today = new Date();
var firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
var daysSinceFirstDay = (today.getTime() - firstDayOfMonth.getTime()) / (1000 * 60 * 60 * 24);
var weekNumber = Math.ceil((daysSinceFirstDay + firstDayOfMonth.getDay() + 1) / 7);

if (weekNumber % 2 == 0) { // Check if it's an even week (every other Saturday)
answer = true; // Run the job
} else {
answer = false; // Don't run the job
}
})();

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

Bhimashankar H
Mega Sage

Hi @Sethu Varma ,

 

Create a scheduled job to run every Saturday, at specific time, and write the below code in advance script condition

 

(function() {

        var answer = false; //This flag will decide to run a job or not
        var weekNumber = new GlideDateTime().getWeekOfYearLocalTime());
    if (weekNumber % 2 == 0)
        answer = true

})();

 

If you schedule now, this will run perfectly because the running weeknumber is 32 so it will run this Saturday and skips next Saturday and runs on next-to-next Saturday and so on.... 

 

For more similar kind of situation, you go throw the below references.

Scheduled job should be trigger Every Month second Tuesday - Solved ,

Trigger scheduled job every month second week - Solved 

 

Thanks,
Bhimashankar H

 

-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!

Ankur Bawiskar
Tera Patron
Tera Patron

@Sethu Varma 

you can check script shared by other members.

share the feedback.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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