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
Bob Lee 14Bob Lee 14 

How to schedule a batch class in every 5 minutes ?

Best Answer chosen by Bob Lee 14
Deepali KulshresthaDeepali Kulshrestha
Hi Bob,
Greetings to you

I have solved your question the BatchUpdateAccountCountField.apxc class is a batch class and BatchUpdateAccountScheduler.apxc class
is scheduler class to schedule BatchUpdateAccountCountField.apxc  class in evety 5 minutes.
 
------BatchUpdateAccountCountField.apxc

global class BatchUpdateAccountCountField implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        String query = 'SELECT Id,Count__c,(SELECT Id FROM Contacts) FROM Account';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc, List<Account> accountList){
        if(accountList.size()>0){
            for(Account accObj : accountList){
                if(accObj.contacts != Null)
                    accObj.Count__c = accObj.contacts.size();
            }
            update accountList;
        }
    }    
    global void finish(Database.BatchableContext bc){
        System.debug('Successfully Updated');
        
    }    
    
}

------BatchUpdateAccountScheduler.apxc
//To schedule above apex class

global class BatchUpdateAccountScheduler implements Schedulable {
    global void execute(SchedulableContext sc){
        BatchUpdateAccountCountField Obj = New BatchUpdateAccountCountField();
        Database.executeBatch(Obj);
    }
    public static void scheduleBatchUpdateAccountScheduler(){
        String cronExp1 = '0 5 * * * ? *';
        String cronExp2 = '0 10 * * * ? *';
        String cronExp3 = '0 15 * * * ? *';
        String cronExp4 = '0 20 * * * ? *';
        String cronExp5 = '0 25 * * * ? *';
        String cronExp6 = '0 30 * * * ? *';
        String cronExp7 = '0 35 * * * ? *';
        String cronExp8 = '0 40 * * * ? *';
        String cronExp9 = '0 45 * * * ? *';
        String cronExp10 = '0 50 * * * ? *';
        String cronExp11 = '0 55 * * * ? *';
        String cronExp12 = '0 0 * * * ? *';
        BatchUpdateAccountScheduler schedulerObj = New BatchUpdateAccountScheduler();
        System.schedule('Update account count Field1', cronExp1, schedulerObj);
        System.schedule('Update account count Field2', cronExp2, schedulerObj);
        System.schedule('Update account count Field3', cronExp3, schedulerObj);
        System.schedule('Update account count Field4', cronExp4, schedulerObj);
        System.schedule('Update account count Field5', cronExp5, schedulerObj);
        System.schedule('Update account count Field6', cronExp6, schedulerObj);
        System.schedule('Update account count Field7', cronExp7, schedulerObj);
        System.schedule('Update account count Field8', cronExp8, schedulerObj);
        System.schedule('Update account count Field9', cronExp9, schedulerObj);
        System.schedule('Update account count Field10', cronExp10, schedulerObj);
        System.schedule('Update account count Field11', cronExp11, schedulerObj);
        System.schedule('Update account count Field12', cronExp12, schedulerObj);
    }
    
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha