0

I have the scheduler class, when I was about to set the Apex scheduler, I noticed that I can only select the time which will be every hour but i need to run it every 5 minutes, can you give me some ideas to achieve this?

This is my scheduler class

global class CaseBreachTAT_Scheduler implements Schedulable{
   global void execute(SchedulableContext sc) {
      CaseBreachTAT_Batch CaseBT_B = new CaseBreachTAT_Batch(); 
      database.executebatch(CaseBT_B);
   }
}
  • @AdrianLarson - this is not a duplicate as the answer you posted to clearly states it cannot be done and only speaks to the UI / cron. At a minimum it does not directly answer the question..I only bring it up because I reviewed that exact answer before posting.. – Eric Aug 12 '16 at 4:26
  • Hmm, it asks how to schedule every 15, and says you should schedule multiple jobs. I feel like I've seen a 5 minute dupe even, just couldn't find it on my phone. – Adrian Larson Aug 12 '16 at 4:47
6

The easiest way is to do this:

  1. Schedule the batch like normal (This just starts it)

  2. In the scheduler class do this to cancel the scheduled job when it runs the first time:

     global void execute(SchedulableContext SC) {
        batchJob batch = New batchJob();
        database.executeBatch(batch);
    
        //delete schedule            
        CronTrigger ct =
            [SELECT Id,
            CronExpression
            FROM CronTrigger
            WHERE Id = :SC.getTriggerId()];
    
    
                //Abort the job. A new one will be scheduled after the batchJob finishes
                System.abortJob(SC.getTriggerId());
    
    
      }
    
  3. Then in your finish method of the batch do this:

    global void finish(Database.BatchableContext BC) {
    
    system.scheduleBatch(New batchJob(),'New Batch Job - ' + DateTime.now().getTime(),15);      
    
    }
    

Note See adrian's comments below. This does not rune every five minutes but chains them 15 minutes after completion.

| improve this answer | |
  • Do I still need to schedule the apex everyday or once the apex has been scheduled (say one day in a week)it will automatically trigger every 5 mins so I dont need to schedule it everyday? – sfdclearner Aug 12 '16 at 3:02
  • It does not matter what you do as long as the schedule executes once. You really do not even need to schedule it. Just execute the batch job and it will schedule itself – Eric Aug 12 '16 at 3:03
  • @gelay - If it works for you please mark as the correct answer – Eric Aug 12 '16 at 4:29
  • So if the batch takes on average two minutes to execute, this will run roughly every seven minutes? – Adrian Larson Aug 12 '16 at 4:49
  • 1
    yup. Safest way to do it IMHO otherwise you have to account for all sorts of other issues, row locks, etc - but good to point out. – Eric Aug 12 '16 at 4:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.