Hello Everyone, In this tutorial, I'll show you, how you can schedule a recurring apex batch job at a particular time or specific time using Apex code. In Salesforce when you schedule a batch job or scheduler class it will run one time in 24 hours but with this example, you can run your scheduler many times within 24 hours. So let's get started

Schedule batch job using apex

Step 1: Login to your Salesforce Org. and open Developer console.

Step 2: Navigate to File | New | Apex Class and Create an Apex controller called MyFirstBatchJob using the following syntax.

MyFirstBatchJob.apxc
 global class MyFirstBatchJob implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC) {              
        return Database.getQueryLocator('SELECT Id, Name FROM Account limit 5');
    }
    
    global void execute(Database.BatchableContext BC, List<sObject> batchObjects) {
        // Write Code logic here                
    }
    
    global void finish(Database.BatchableContext BC) {
        // Get the ID of the AsyncApexJob representing this batch job from Database.BatchableContext.
        // Query the AsyncApexJob object to retrieve the current job's information.
        AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,  TotalJobItems, CreatedBy.Email, ExtendedStatus FROM AsyncApexJob WHERE Id = :BC.getJobId()];                
        if(a.NumberOfErrors > 0) {  
           // Send an email to the Apex job's submitter notifying of job completion if there are errors.
        }  
    }
 }
Step 3: Navigate to File | New | Apex Class and Create an Apex controller called MyFirstBatchJobScheduler. Replace following code in apex controller.

MyFirstBatchJobScheduler.apxc
 global class MyFirstBatchJobScheduler implements Schedulable{
    global void execute(SchedulableContext SC){ 
        Database.executebatch(new MyFirstBatchJob());
    }
 }
Step 4: Navigate to File | New | Apex Class and Create an Apex controller called MyFirstBatchJobSchedulerHourlyReplace following code in apex controller.

MyFirstBatchJobSchedulerHourly.apxc
 global class MyFirstBatchJobSchedulerHourly implements Schedulable{    
    global void execute(SchedulableContext SC){  
        Datetime sysTime = System.now().addHours(1); 
        String chronExpression = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();
        System.schedule('MyFirstBatchJob'+ sysTime, chronExpression,new MyFirstBatchJobScheduler());
        MyFirstBatchJobSchedulerHourly.Start();
    } 
    
    public static void Start(){
        // start keepalive again in 1 hours
        Datetime sysTime = System.now().addHours(1); 
        String chronExpression = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();
        if(!Test.isRunningTest())
        System.schedule('MyFirstBatchJobkeepAlive'+ sysTime, chronExpression, new MyFirstBatchJobSchedulerHourly());
    }    
 }
Above class that I created in Step-4 will schedule your batch job on hourly basis. There is no need to schedule you apex batch job manually, you just have to execute this class in the anonymous window. To execute navigate to Debug | Open Execute Anonymous Window  or press CTRL+E, paste the below code in the popup window and click on Execute button
 MyFirstBatchJobSchedulerHourly cls=new MyFirstBatchJobSchedulerHourly();
 cls.execute(null);
To see your schedule job please navigate to Your Name | Setup | Jobs | Schedule Jobs. The result will be:


See also:

Conclusion:
Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.