We have a nightly job that will update opportunities for every record in a custom object.

Since that custom object will contain only the "updates in the last 24 hrs", we do not expect the volume to be more than 500 records.

So it all boils down to updating around 500 opportunity records in a single job invocation.

Can't I just use an update via a regular apex class instead of building batch apex for this ?

What will be the factors due to which batch/regular apex choice is determined ?

1 Answer 1


All automated periodic jobs, no matter how small or trivial they may seem, should consider running as a batch or at least a chained Queuable, or offer a dynamic choice on how to behave. For example, you say it may run nightly with up to 500 rows, but what happens if someone cancels the job, and forgets to restart it, and nobody notices for two weeks? Now you need to process 7,000 rows. Will your job still run, or will it fail? What if you have an unexpected burst of data and you suddenly need to process 75,000 rows?

The easiest way to "future-proof" yourself is to make a decision at run-time. Pick a threshold you're comfortable with (perhaps even by way of custom setting), and make a prediction based on the volume to process:

public void execute(SchedulableContext context) {
    Integer myLimit = (from custom setting or metadata);
    MyObj__c[] records = [SELECT Id... FROM MyObj__c LIMIT :myLimit];
    if(records.size() == myLimit) {
        // Too many rows here, so go to batch mode
        Database.executeBatch(new MyObjBatch());
    } else {
        new MyObjBatch().execute(null, records);

It's assumed that MyObjBatch contains an execute method from Database.Batchable, so you can pass in a null BatchableContext value to indicate it is running within the scheduler.

  • Learnt something new today that we can pass null for batchablecontext. Thanks!
    – javanoob
    Commented Mar 10, 2016 at 3:56
  • Might be cleaner to add an overload to the batch that accepts just List<SObject> and passes the null for you. Still, pretty cool. I always figured null BatchableContext would fail.
    – Adrian Larson
    Commented Mar 10, 2016 at 15:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .