function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Schedule Apex class after every 2 weeks?



Can anyone please help me with the cron expression to schedule apex class after every 2 weeks?






The format for the scheduler string is:

seconds, minutes, hour of day, day of month, month, day of week

Try this: the 1,15 means to schedule on the 1st and 15th of the month.

'0 0 22 1,15 * ?';


        MyScheduler myScheduler = new MyScheduler();
        String schedule = '0 0 22 1,15 * ?';
        system.schedule('Scheduled Job', schedule , myScheduler);
        message = 'Job scheduled for 10pm on 1st and 15th of every month.';



Hi Ram,

Thanks for your quick response.

If we schedule on every 1 and 15 day of month, then it won't exactly takes 2 weeks difference having 31 days or feb month.

So, for example, if I schedule on Monday, then it should be run on next to next Monday.



Rupali, You can run it every day or every week and do the actual work it is supposed to do every 2 weeks. To achieve this you'll need to create may be a custom setting to store the date of the last run. Every time it runs, calculate the number of days from the last work run - if you are 14 days from then, do the work and update your settings with today's date. Hope that helps.