How to schedule apex class for 5 or 10 minutes

Want to learn How to schedule apex class?, if yes then this article is for you.

Today we are going to follow an easy guide which will demonstrate how can we create a schedule apex class step by step.

How to schedule Apex Class
How to schedule Apex Class

Now, we are going to see how can we schedule Apex class in two simple steps. First we need to understand the concept why we need a code when by default salesforce provides options for scheduling.

To schedule apex, we need to create one Schedulable Apex class which you can see in step 1 below. Now to schedule this we are going to use default options given by salesforce in this case we can only schedule the class for 1 hour interval not less then that and due to this limitation we use a piece of codes which helps us to schedule apex classes for n number of minutes. Follow Step 1 and 2 for basic understanding and later we will see the coding part.

Step 1: Create Schedulable Apex

First of all, you have to have your apex class in a schedulable format. To achieve that, you need to create a new apex class with the following code:

Java
global class SomeClass implements Schedulable {
    global void execute(SchedulableContext ctx) {
        //  code here
    }
}

Note: You can read more about scheduling Apex on SalesForce Trailhead.

Step 2: Scheduling Apex in a standard way

Go to Setup -> Apex Classes. From there you’ll be able to see that there is a button that is called ‘Schedule Apex‘

How to schedule apex class for 5 or 10 minutes
Scheduling Apex in a standard way

Note: The truth is, Salesforce allows you to schedule APEX to run every hour. But what if you need your code to run every 15 minutes?

How to schedule an apex schedulable class to run after every 10 or 15 minutes

There are scenarios in which we need to schedule an apex class to run after every 10 minutes or 15 minutes. In order to achieve this requirement, we can create 2 apex scheduler classes. Whenever the first scheduler will run, it will perform processing or operation and after that it will schedule a second apex schedule class to run after specified interval (in below example, we have mentioned 10 minutes). When the second scheduler will run it performs operation or processing and will schedule the first apex schedule class to run after specified interval.

Step 1: Apex Scheduler Class:

Java
global class Scheduler01 implements Schedulable {
    global void execute(SchedulableContext SC)     
    {
        //Call other class method which will perform operations as it is 
        //recommended that all processing take place in a separate class
        doScheduling();
    }
    public void doScheduling(){
       try{
            dateTime dt=System.now().addMinutes(10); //you can specify 10 mins or 15     
                                                                              //mins or any interval
            String Csec,Cmin,Chr,Cday,Cmonth,CYear;
            Csec=String.valueof(dt.second());
            Cmin=String.valueof(dt.minute());
            Chr=String.valueof(dt.hour());
            Cday=String.valueof(dt.day());
            Cmonth=String.valueof(dt.month());
            CYear=String.valueof(dt.Year());
            String SchTimer=Csec+' '+Cmin+' '+Chr+' '+Cday+' '+Cmonth+' ? '+CYear;
            system.debug('*************SchTimer:'+SchTimer);
            Scheduler02 cas = new Scheduler02();
            system.schedule('Scheduler02: Running at '+System.now().format(), SchTimer, cas);
            //we will delete completed apex scheduled jobs for which state is DELETED
            for( CronTrigger c:[Select State,Id,EndTime,CronExpression From CronTrigger where  
                                        NextFireTime= null  AND State='DELETED' Limit 100]){
                    System.abortJob(c.id);
            }
        }
        catch(exception e){
        }
   }
}

Note: When you save the above code you might get an error because Scheduler02 class is not created to solve this just comment below line from Scheduler01 apex classes and save it.

Scheduler02 cas = new Scheduler02();

system.schedule(‘Scheduler02: Running at ‘+System.now().format(), SchTimer, cas);

After saving Scheduler01 you can save Scheduler02 class and then uncomment the above mentioned lines from Scheduler01 class and save it.

Step 2: Apex Scheduler Class:

Java
global class Scheduler02 implements Schedulable {
    global void execute(SchedulableContext SC){
        //Call other class method which will perform operations as it is 
        //recommended that all processing take place in a separate class
        doScheduling();
    }
    public void doScheduling(){
      try{
            dateTime dt=System.now().addMinutes(10);
            String Csec,Cmin,Chr,Cday,Cmonth,CYear;
            Csec=String.valueof(dt.second());
            Cmin=String.valueof(dt.minute());
            Chr=String.valueof(dt.hour());
            Cday=String.valueof(dt.day());
            Cmonth=String.valueof(dt.month());
            CYear=String.valueof(dt.Year());
            String SchTimer=Csec+' '+Cmin+' '+Chr+' '+Cday+' '+Cmonth+' ? '+CYear;
            system.debug('*************SchTimer:'+SchTimer);
            Scheduler01 cas = new Scheduler01();
            system.schedule('Scheduler01: Running at '+System.now().format(), SchTimer, cas);
            //we will delete completed apex scheduled jobs for which state is DELETED
            for( CronTrigger c:[Select State,Id,EndTime,CronExpression From CronTrigger  
                                where NextFireTime=null  AND State='DELETED' Limit 100]){
                    System.abortJob(c.id);
            }
        }
        catch(exception e){
        }
    }
}

Was all that confusing and didn’t helped you what you were looking no worries we have worked out to find some other approaches as well.

Easiest way to Schedule apex class every 5 minutes in salesforce

Java
System.schedule('Schedule Job Name 1',  '0 00 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 2',  '0 05 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 3',  '0 10 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 4',  '0 15 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 5',  '0 20 * * * ?', new ScheduleBatchApexClassExample());

The above code starts with ‘0 00’ and then ‘0 05’ which shows that the Scheduler is running for every 5 minutes which can be seen from the increment order.

Easiest way to Schedule apex class every 10 minutes in salesforce

Java
System.schedule('Schedule Job Name 1',  '0 00 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 2',  '0 10 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 3',  '0 20 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 4',  '0 30 * * * ?', new ScheduleBatchApexClassExample());
System.schedule('Schedule Job Name 5',  '0 40 * * * ?', new ScheduleBatchApexClassExample());

By now we hope you got the understanding of how this scheduling concept works in terms of 10 minutes it will increment by 10, an so on for other n minutes as well.

Conclusion

Do let us know if we have missed anything or we made any mistake in explaining the concepts. Comment down below to share your feedbacks. Stay active on our site for more salesforce topics and exciting interview questions.

Salesforce Related:

Leave a Comment

10 − five =