1

Is it possible to set the StartTime and EndTime fields of a CronTrigger object ?

I'm not able to set this fields in Apex code, I have the error below :

Field StartTime is not writeable

I know I can do it directly in CRON expression but only for simple expression.

For example, if I want to schedule a job every Monday at 2:00PM between 23th March 2015 and 12th January 2016, how can I do it with a CRON expression ?

When I schedule an APEX class in Salesforce GUI, I can choose the startTime and EndTime, so I think it's possible !

Any ideas ?

1 Answer 1

4

Lets say you want to schedule a class which run from 20-August-2015 to 30-August-2015 daily at 7 AM. Cron expression would be:

0 0 7 20-30 8 2015

Lets say you want to schedule a class which run from 20-August-2015 to 20-August-2016 daily at 7 AM. Cron expression would be:

0 0 7 20 8 2015-2016

Cron for running a batch every Monday at 2:00 PM is

 0 0 14 ? * 2

You can not write directly to fields. You will have to reschedule the class and delete old schedule.

2
  • Ok, thanks. But in your examples, you have dates in the same month or in the same year. In my example, I would like to do every Monday at 2:00PM between 23th March 2015 and 12th January 2016. Is there possible to do this type of complex interval with a CRON expression ?
    – SF_user
    Commented Aug 20, 2015 at 7:38
  • 1
    @SF_user You will need to create two schedules. one schedule will run from 23 march 2015 to Dec 2015 & 2nd schedule will run for 1st January 2016 to 12th January. Cron for running a batch every Monday at 2:00 PM is 0 0 14 ? * 2
    – Ashwani
    Commented Aug 20, 2015 at 8:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .