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
o2bo2b 

How to schedule jobs every 10 minutes

Current documents has no example on how to schedule apex jobs every 10 mins. I tried with unix cron job syntax style (like 0,10,20,30,40,50 slots), that doesn't work.

 

Any help would be appreciated.

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

Below code will autometically schedule job for every 10 minutes. You just need to first time invoke this method.

 

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() + 10);
        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());
    } 
}

 

All Answers

paul-lmipaul-lmi
i'm pretty sure the lowest you can go is daily
ColinKenworthy2ColinKenworthy2
In the Apex docs read the section on Scheduler Best Practices.
o2bo2b
As per the document you can schedule every hour or day or month. But nothing mentioned on scheduling every 10 mins or 15 mins.
ColinKenworthy2ColinKenworthy2

Try setting up 4 schedules; hourly at 00, hourly at 15, hourly at 30, hourly at 45.

You can have max 10 schedules per org.

paul-lmipaul-lmi
If scheduler is anything like escalation rules, that'd get messy.  They don't guarantee that the scheduled job will kick off at the precise time you schedule it, but rather, somewhere "close" to it.
ColinKenworthy2ColinKenworthy2
Perhaps your job as it finishes could add another schedule for 10mins after the current time?
jhartfieldjhartfield
Colin:  I tried implementing such a solution, which worked, except after you reach 10 jobs it fails.   And there is no way that I have found to programatically delete jobs.  The old completed jobs seem to count against your 10 job limit.
o2bo2b
Can you post any sample code? Thanks!
sf11sf11

You can schedule the Apex class every hour (max 10) but I don't see any option to schedule on a frequency less than hour.

 

Let's if you want to schedule a class e.g. myClass.somemethod() you coding the scheduler class provided myClass implements schedulable

 

global class myClass implements Schedulable{

            global void execute(SchedulableContext SC) {

 

//Inside code for someMethod() 

 

 

Once you have created the schedulable class you can simply go to  App Setup--> Apex Classes and click schedule button to create a scheduled job.

 

The repeat the job more than once a day, create a new schedule for the same myClass.someMethod() . For example first schedule at 10:00 AM, next 11:00 AM , next 12:00 noon and so on. 

 

I didn't see any option to schedule something like 11:10, 11:20:11:30 etc. I don't think this is even possible as of today. 

ascuccimarraascuccimarra

In order to schedule a job to run every 10 minutes you need to schedule 6 different jobs, since with spring 10' release, the min interval in which a job can run is every hour. You should run this code from the system log:

 

 YourScheduleClass c = new YourScheduleClass();
String sch = '0 0 * * * ?';
System.schedule('Schedule Job1', sch, c);

YourScheduleClass c = new YourScheduleClass();
String sch = '0 10 * * * ?';
System.schedule('Schedule Job2', sch, c);

YourScheduleClass c = new YourScheduleClass();
String sch = '0 20 * * * ?';
System.schedule('Schedule Job3', sch, c);

YourScheduleClass c = new YourScheduleClass();
String sch = '0 30 * * * ?';
System.schedule('Schedule Job4', sch, c);

YourScheduleClass c = new YourScheduleClass();
String sch = '0 40 * * * ?';
System.schedule('Schedule Job5', sch, c);

YourScheduleClass c = new YourScheduleClass();
String sch = '0 50 * * * ?';
System.schedule('Schedule Job6', sch, c);

Sam_IndiaSam_India

Check my first and recent post mate:

http://community.salesforce.com/t5/Apex-Code-Development/How-to-create-a-Schedule-Job-which-runs-after-specific-interval/m-p/178565

Mail me at ssaini@astadia.com if need any help on this.

 

Thanks,

Sam

yuvrajindiayuvrajindia

I can scheduled every 10 min by helping this code but now problem is after 10 job it will give limit error.

 

how can i sheduled more then 10 job or after completing  delete this job so we can sheduled new job.

 

Thanks

Yuvraj

o2bo2b

Is there anyway to KILL a job from Apex code?

kardoluskardolus

Yeah, use getTriggerId() from the SchedulableContext reference variable. It will return an Id wich you can pass to System.abortJob()

Dhaval PanchalDhaval Panchal

Below code will autometically schedule job for every 10 minutes. You just need to first time invoke this method.

 

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() + 10);
        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());
    } 
}

 

This was selected as the best answer
ValueText SMSValueText SMS
Above code after bug fix


         datetime dt=system.now().minute() >54 ? system.now().addMinutes(6) : system.now();
       
         //RecurringScheduleJob.startJob();   
        String day = string.valueOf(dt.day());
        String month = string.valueOf(dt.month());
        String hour = string.valueOf(dt.hour());
        String minute = string.valueOf(dt.minute() + 5);
        String second = string.valueOf(dt.second());
        String year = string.valueOf(dt.year());
        
        String strJobName = 'Job-' + second + '_' + minute + '_' + hour + '_' + day + '_' + month + '_' + year;
        String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
        system.debug(strSchedule+'Time stamp');
     
Christina ZhankoChristina Zhanko
Hi! Our team developed package for resilving similar problems!
Schedule manager on AppExchange - https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000DqCmYUAV. Can execute your schedule periodically, like you want. You can configure run time every 5 minutes, every hour, once a month, etc.
Siddharth Birari 6Siddharth Birari 6
@Dhaval: In now ways your code is wrong, however with this code, you leave your apex class with no flexibility than to schedule it every 10 mins only. In case, in future I need to change the schedule from every 10 mins to say every hour, I need to change the code again. 

The most appropriate solution for this I believe would be to have separate schedulers.
Om GuptaOm Gupta
only issue with above code is when minute value contains 60  then it will throw the excpetion that "minute value will be between [0 - 59]
for example if i am executing the scheduler every 1 min and when the system.now().minute() + 1= 60 then scheduler will be stop. we have only one changes 
DateTime now = System.Now().addMinutes(1);//you can add any minute here and you have not to check the condtion also that it become 60 or not
String day = string.valueOf(now.day());
String month = string.valueOf(now.month());
String hour = string.valueOf(now.hour());
String minute = string.valueOf(now.minute());
String second = string.valueOf(now.second());
String year = string.valueOf(now.year());
String cronExpression = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;
System.schedule(strJobName, cronExpression, new scheduledTest());

hope it will help