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
v varaprasadv varaprasad 

Schedule Apex with different timings

Hi Everyone.

  i need small help regarding on schedule apex.
  how we will schedule one class with different timings.
EX:i want to schedule one class :  Runs at 4:20; 7:50: 16:20; 20:50 CET these timings.

Thanks in Advance


 
Alexander TsitsuraAlexander Tsitsura
Hi Varaprasad,

For implement this requirement, you can create list of times and pass this as parametr to shedule job constructor.
Time[] times = new Time[] {
   Time.newInstance(4,20,0,0),
   Time.newInstance(7,50,0,0),
   Time.newInstance(16,20,0,0),
   Time.newInstance(20,20,0,0),
};
public class scheduledClass implements Schedulable {
   
  private Time[] times;
  private Integer currIndex;

   public scheduledClass(Time[] times) {
      this.times = times;
      this.currIndex = 0;
   }

   public scheduledClass(Time[] times, Integer currIndex) {
      this.times = times;
      this.currIndex = currIndex;
   }

   public void execute(SchedulableContext sc) {
	final String jobId = String.valueOf(sc.getTriggerId());
	System.abortJob(jobId);

        // do logic

       // reshedule job
       final DateTime d = DateTime.newInstance(Date.today(), times[currIndex]);
       if (d < DateTime.now()) d.addDays(1);

       // increment index of times
       currIndex = Math.mod((currIndex + 1), times.size());
       System.schedule(
	'JOB_NAME',
	d.format('ss mm HH dd MM ? yyyy'),
	new scheduledClass(times, currIndex)
      );
   }
}

Also you can save times in custom settings.


Thanks,
Alex
v varaprasadv varaprasad
Hi Alex.

 Thanks for your reply..it is more help full for me.