2

I would like to have my scheduled apex go off twice month for example every 1st and 15th of the month. On the 1st I would like for it to run x amount of times and stop when x is met. And then not run again until the 15th and run again for x amount of times and stop when x is met. I am not sure if this is possible? Ive done my own research and all I can find is every hour, every day, every week etc.

Thank you in advance

Derrick

  • What is "X" that you're trying to meet? – sfdcfox Dec 14 '17 at 23:10
  • i would like to run it 24 times. – DerrickE Dec 14 '17 at 23:13
  • You can schedule 24 jobs, 30 mins apart. At midnight, then 12:30, then 1, 1:30 and so forth. You can even go more granular down to seconds, though I prefer to spread them apart due to concurrency and database locks – Boris Bachovski Dec 14 '17 at 23:17
  • or simply create a job than can run and evaluate your terminator condition and remove the job from the schedule and reschedule it on the 15th or on the 1st - whichever comes first – Ronnie Jul 3 '18 at 2:40
1

You can schedule multiple jobs for the same day at different times. If you want to run it 4 times on the 1st of the month, schedule it at 4 different times (create 4 jobs):

'0 0 1 1 * ?' // 1am on the 1st of every month
'0 0 2 1 * ?' // 2am on the 1st of every month
'0 0 3 1 * ?' // 3am on the 1st of every month
'0 0 4 1 * ?' // 4am on the 1st of every month

Same thing for the 15th of every month

'0 0 1 15 * ?' // 1am on the 15th of every month
'0 0 2 15 * ?' // 2am on the 15th of every month
'0 0 3 15 * ?' // 3am on the 15th of every month
'0 0 4 15 * ?' // 4am on the 15th of every month

You get the idea. Everything is explained here.

| improve this answer | |
  • 1
    Awesome thanks Boris. I need to study up on Cron expressions. – DerrickE Dec 14 '17 at 23:14
3

You can generally use all sorts of nifty things beyond the second and minutes. For example, you can schedule a job like this:

0 0 * 1,15 * ?

Which would run every hour on the first and fifteenth of every month. You should check the documentation for other possible combinations.

Note that the documentation says you can't specify wildcards for hours... but this is (at least not yet) how it works. It may be an oversight in the documentation, or perhaps a future limitation, but for now, it works as expected.

| improve this answer | |
  • awesome thanks. I def need to read up on the documentation. Cron expressions is new to me. – DerrickE Dec 14 '17 at 23:18
  • If 'X' is static (24) then this is the preferred way, otherwise multiple jobs need to be scheduled. – Boris Bachovski Dec 14 '17 at 23:18
  • 1
    @BorisBachovski Not necessarily. You can specify hours by commas, such as 0 0 1,3,7,10 1,15 * ? to run at 1, 3, 7, and 10 am on the 1st and 15th of each month. It's only when you need to deviate on the minutes or seconds that you need separate schedules. – sfdcfox Dec 14 '17 at 23:20
  • True, as long as you don't have to run it more than 24 times :) – Boris Bachovski Dec 14 '17 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.