How do we schedule a class to run every 15 minutes in salesforce?
Asked Answered
N

2

8

I am trying to schedule a class to run every 15 minutes. I know we can set in salesforce for every hour, but is there a way to reduce the granularity to 10-15 minutes?

global class scheduledMerge implements Schedulable {
  global void execute(SchedulableContext SC) {
      ProcessTransactionLog p= new ProcessTransactionLog();
      p.ProcessTransaction();
   }
}
Nicks answered 3/2, 2012 at 10:9 Comment(0)
I
14

You can use this apex code snippet to schedule your job to run every 15 minutes.

System.schedule('Job1', '0 0 * * * ?', new scheduledMerge());
System.schedule('Job2', '0 15 * * * ?', new scheduledMerge());
System.schedule('Job3', '0 30 * * * ?', new scheduledMerge());
System.schedule('Job4', '0 45 * * * ?', new scheduledMerge());
Ita answered 3/2, 2012 at 15:15 Comment(4)
I'm pretty sure you can also use commas to delimit multiple values: System.schedule('Job1', '0 0,15,30,45 * * * ?', new scheduledMerge());Rosamondrosamund
The first line of this answer will cause the job (theoretically) to execute every minute. The "cron-preferred" way to express "run every 15 minutes on the 15" would be '0 0/15 * * * ?'.Thomey
Matt, actually in Apex CRON you must specify Seconds and Minutes as Integers --- commas, stars, and dashes are only available for the other CRON components. So your '0,15,30,45' approach will not work. Also, a word of caution about following Rajesh's approach: you can only have 25 total Scheduled Apex Classes, and Rajesh's approach uses up 4 of them. But if you have this many jobs to spare, then this will work great.Milter
System.schedule('Job1', '0 0 * * * ?', new scheduledMerge());Zulazulch
B
0
global class scheduledTest implements Schedulable {
    global void execute(SchedulableContext SC) {
        RecurringScheduleJob.startJob();   
        String day = string.valueOf(system.now().day());
        String month = string.valueOf(system.now().month());
        String hour = string.valueOf(system.now().hour());
        String minute = string.valueOf(system.now().minute() + 15);
        String second = string.valueOf(system.now().second());
        String year = string.valueOf(system.now().year());

        String strJobName = 'Job-' + second + '_' + minute + '_' + hour + '_' + day + '_' + month + '_' + year;
        String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
        System.schedule(strJobName, strSchedule, new scheduledTest());
    } 
}
Blende answered 9/10, 2013 at 13:55 Comment(2)
Define a variable called 'now' and use addMinutes(Integer) method will be accurate. Datetime now = System.now(); Datetime nextE = now.addMinutes(10);Zulazulch
this will run every 15 minutes even when i set the apex schedule to 1 hour?Araiza

© 2022 - 2024 — McMap. All rights reserved.