Activity Forums Salesforce® Discussions How to schedule a batch class in the same batch class in Salesforce?

  • Prachi

    Member
    September 26, 2019 at 10:20 am

    Hi,

    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);
    }
    }

    Thanks.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos