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
suhas ghule 3suhas ghule 3 

StartTime and EndTime in Cron Expression

I have a cron expression which will schedule an apex class to run on every SUNDAY
The cron Expression for that is - 0 0 0 ? * SUN *

But i want to include a StartDate and EndDate to that cron expression.
Suppose the StartDate(dd/mm/yyyy) - 12/9/2014,  EndDate- 26/9/2014
The cron should only run on sundays between these two dates

Any ideas on how to do that?? 
Vatsal KothariVatsal Kothari
Hi Suhas,

You can do this by writing apex class which implements schedulable interface.

You can schedule that apex class on weekly or monthly basis, also you can specify start and end date.

you can refer below links:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

http://www.salesforce.com/us/developer/docs/apex_workbook/Content/apex_scheduling_1.htm

https://help.salesforce.com/HTViewHelpDoc?id=code_schedule_batch_apex.htm&language=en_US

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
suhas ghule 3suhas ghule 3
Thanks for the effort but that's not what i want to know.
Let me rephrase my problem-
I'm scheduling my class using System.Schedule('blah blah', CRON_EXP,className);
CRON_EXP== '0 0 0 ? * SUN *'  --> what this does is schedule the class on every sunday irrespective of the StartDate and EndDate.
for eg. Today's Date--> 9/10/2014
             StartDate--> 9/15/2014
             EndDate--> 9/23/2014
So, according to my StartDate & EndDate, the next fire time should be 9/21/2014. But here the nextFireTime is 9/14/2014.
How to get around with this problem??


Vatsal KothariVatsal Kothari
If you have seen the example of the above link,
you have to create Batch class which implements schedulable interface, perform all your task inside batch class.
After saving Batch class to go to setup > Develop > Apex Classes 
There is a Button called Schedule Apex click on this and u'll get solution.

Thanks,
Vatsal
suhas ghule 3suhas ghule 3
Okay, you need to stop replying me. You clearly didn't understand I WANT TO SCHEDULE THE CLASS VIA APEX CODING i.e using *System.schedule().*
AK VineethAK Vineeth
HI  Sushas 

In Order to Schedule a class from apex class use the following syntax 


    here is a small Example where in am scheduling an apex class 
public class Scheduler {
   
    public void doScheduling(){
      try{
            dateTime dt=System.now().addMinutes(10);
            String Csec,Cmin,Chr,Cday,Cmonth,CYear;
            Csec=String.valueof(dt.second());
            Cmin=String.valueof(dt.minute());
            Chr=String.valueof(dt.hour());
            Cday=String.valueof(dt.day());
            Cmonth=String.valueof(dt.month());
            CYear=String.valueof(dt.Year());
            String SchTimer=Csec+' '+Cmin+' '+Chr+' '+Cday+' '+Cmonth+' ? '+CYear;
            system.debug('*************SchTimer:'+SchTimer);
            Scheduler01 cas = new Scheduler01();
            system.schedule('Scheduler01: Running at '+System.now().format(), SchTimer, cas);
           // Here we use system.schedule to run the schedule class with following parameters
        }
        catch(exception e){
        }
    }
}


and try to implement the logic ,which runs as per your desired conditions ..

Hope this helps you 

Cheers