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
Souravmoy GoraiSouravmoy Gorai 

Cron Expression on specific day between a specific period of time

Hi All,

I have to schedule an Apex class, using a custom schedule page. I have to write the CRON EXPRESSION based on the requirements like :
It should be scheduled between specific days (like, we would have a start date and an end date) along with on specific days of the week (like, on Saturday or Sunday); i.e. something like Schedule an apex class on Sundays between 12/05/2016 to 12/06/2016.
Please help.
Thanks in advance.
Best Answer chosen by Souravmoy Gorai
Rohit K SethiRohit K Sethi
Hi Souravmoy Gorai,
/*
for your above requirment execute this code in your class or where you want to execute scheduled.
*/

//Bind your Start Date and End Date
Date StartDate  = System.today();
Date EndDate = System.today().addDays(30); 
//Set Week Name which is you received from page
String weekName = 'Sunday';
//Caculating the differnece between days.
Integer daysBetween = startDate.daysBetween(endDate);

startDate  = startDate.addDays(-1);
//Schedule class Name which you want to be execute
ScheduleClass  c = new ScheduleClass();

for(Integer i = 0 ;i < daysBetween ;i++){
     
	Date newDate = startDate.addDays(i);
	Datetime dt = datetime.newInstance(newDate.year(), newDate.month(),newDate.day());
	String dayOfWeek= dt.format('EEEE');
	if(dayOfWeek.toLowerCase() == weekName.toLowerCase()){
		System.debug('Inside Day --->' + dayOfWeek);
        String sch = '0 0 0 '+ newDate.day() + ' ' + newDate.month() + ' ? ' + newDate.year();
		System.schedule('Visit unique' + i + newDate, sch, c);
		i = i + 6;
	}	
}
If this post solves your problem kindly mark it as solution.
Thanks.
 

All Answers

Rohit K SethiRohit K Sethi
Hi Souravmoy Gorai,
/*
for your above requirment execute this code in your class or where you want to execute scheduled.
*/

//Bind your Start Date and End Date
Date StartDate  = System.today();
Date EndDate = System.today().addDays(30); 
//Set Week Name which is you received from page
String weekName = 'Sunday';
//Caculating the differnece between days.
Integer daysBetween = startDate.daysBetween(endDate);

startDate  = startDate.addDays(-1);
//Schedule class Name which you want to be execute
ScheduleClass  c = new ScheduleClass();

for(Integer i = 0 ;i < daysBetween ;i++){
     
	Date newDate = startDate.addDays(i);
	Datetime dt = datetime.newInstance(newDate.year(), newDate.month(),newDate.day());
	String dayOfWeek= dt.format('EEEE');
	if(dayOfWeek.toLowerCase() == weekName.toLowerCase()){
		System.debug('Inside Day --->' + dayOfWeek);
        String sch = '0 0 0 '+ newDate.day() + ' ' + newDate.month() + ' ? ' + newDate.year();
		System.schedule('Visit unique' + i + newDate, sch, c);
		i = i + 6;
	}	
}
If this post solves your problem kindly mark it as solution.
Thanks.
 
This was selected as the best answer
Sai Sharanyya ChandaSai Sharanyya Chanda
Day by day we need to schedule by using cron expression in salesforce. How can we achieve it?