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
Jakson MonteiroJakson Monteiro 

How to write batch class and schedule class in the same class?

Can we write a batch class and schedule class in the same class. is it possible ?
ManojjenaManojjena
Hi Jakson,
We can write both batch and schedule in one class as both are interface we can implement more then one interface in one class which is basically known as multiple inheritance .
You can try like below !!
global class BatchAndSchedule implements Schedulable,Database.Batchable<sObject>{
   global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT Id,Name FROM Account';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Account> scope){
         for(Account acc : scope){
             acc.Name = acc.Name +'ManojBatch';            
         }
         update scope;
    }   
    global void finish(Database.BatchableContext BC){ }
    global void execute(SchedulableContext scon) {
      Database.executeBatch(new BatchAndSchedule(),100);
   }

}
Let me know if it helps!!!
Thanks
Manoj