How to update classes even when they are scheduled in Salesforce?

How to update classes even when they are scheduled in Salesforce?

Sample Batch Class:


global class AccountUpdateBatch implements Database.Batchable <sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        String SOQL = ‘SELECT Id, Description FROM Account’;
        return Database.getQueryLocator(SOQL);
    }
    
    global void execute(Database.BatchableContext bc, List<Account> listAcct) {
        for(Account acct : listAcct) {
            acct.Description = String.valueOf(system.today());
        }
        update listAcct;
    }
    
    global void finish(Database.BatchableContext bc) {
    }
}


Scheduler Class:


global class AccountUpdateScheduler Implements Schedulable {
    public Interface AccountUpdateSchedulerInterface {
        void execute(SchedulableContext sc);
    }
    global void execute(SchedulableContext sc) {
        Type targetType = Type.forName(‘AccountUpdateBatchSchedulerHndlr’);
        if(targetType != null) {
            AccountUpdateSchedulerInterface obj = (AccountUpdateSchedulerInterface)targetType.newInstance();
            obj.execute(sc);
        }
    }
}


Scheduler Handler Class:


public class AccountUpdateBatchSchedulerHndlr implements AccountUpdateScheduler.AccountUpdateSchedulerInterface {
    public void execute(SchedulableContext sc) {
        AccountUpdateBatch obj = new AccountUpdateBatch();
        Database.executeBatch(obj);
    }    
}

To check whether the batch class is executed by scheduling the scheduler class, execute the below code in Developer console or in Workbench.

        Datetime dt = Datetime.now().addMinutes(1);
        String CRON_EXP = ‘0 ‘+ dt.minute() + ‘ * ‘ + dt.day() + ‘ ‘ + dt.month() + ‘ ? ‘ + dt.year();
        String jobId = System.schedule(‘AccountUpdateScheduler’, CRON_EXP, new AccountUpdateScheduler() ); 

Schedule the AccountUpdateScheduler class and try to update the handler class or batch class. We will not get any error stating that dependent class is scheduled.

Cheers!!!

Leave a Reply