Skip to main content
added 398 characters in body
Source Link
Adrian Larson
  • 151.4k
  • 38
  • 247
  • 431

I highly recommend you just roll scheduling functionality into your batches and strip out all the cron information from your class. IMO that really belongs in the context where you schedule the job in the first place.

public class MyBatch implements Database.Batchable, Scheduleable
{
    public Integer batchSize = 200;
    public void execute(ScheduleableContext context)
    {
        Database.execute(this, batchSize);
    }
    // batchable implementation
}

You could even put this scheduling into an abstract class (e.g. ScheduleableBatch) and extend that instead of including this boilerplate in every batch you write. If you follow this pattern for both batch implementations, you would do something like:

system.schedule('Job A', '0 0 * * * ?', new ScheduleableBatchA());
system.schedule('Job B', '0 0 * * * ?', new ScheduleableBatchB());

If you really want to implement it using the approach you've started already, you need to add the relevant property as state. I recommend you cache a Type rather than a String.

public with sharing class MyBatchScheduler implements Scheduleable
{
    final Type batchType;
    final Integer batchSize;
    public MyBatchScheduler(Type batchType) { this(batchType, 2000); }
    public MyBatchScheduler(Type batchType, Integer batchSize)
    {
        this.batchType = batchType;
        this.batchSize = batchSize;
    }
    public void execute(ScheduleableContext context)
    {
        Database.Batchable<SObject> batch =
            (Database.Batchable<SObject>)batchType.newInstance();
        Database.executeBatch(batch, batchSize);
    }
}

I highly recommend you just roll scheduling functionality into your batches and strip out all the cron information from your class. IMO that really belongs in the context where you schedule the job in the first place.

public class MyBatch implements Database.Batchable, Scheduleable
{
    public Integer batchSize = 200;
    public void execute(ScheduleableContext context)
    {
        Database.execute(this, batchSize);
    }
    // batchable implementation
}

You could even put this scheduling into an abstract class (e.g. ScheduleableBatch) and extend that instead of including this boilerplate in every batch you write. If you follow this pattern for both batch implementations, you would do something like:

system.schedule('Job A', '0 0 * * * ?', new ScheduleableBatchA());
system.schedule('Job B', '0 0 * * * ?', new ScheduleableBatchB());

I highly recommend you just roll scheduling functionality into your batches and strip out all the cron information from your class. IMO that really belongs in the context where you schedule the job in the first place.

public class MyBatch implements Database.Batchable, Scheduleable
{
    public Integer batchSize = 200;
    public void execute(ScheduleableContext context)
    {
        Database.execute(this, batchSize);
    }
    // batchable implementation
}

You could even put this scheduling into an abstract class (e.g. ScheduleableBatch) and extend that instead of including this boilerplate in every batch you write. If you follow this pattern for both batch implementations, you would do something like:

system.schedule('Job A', '0 0 * * * ?', new ScheduleableBatchA());
system.schedule('Job B', '0 0 * * * ?', new ScheduleableBatchB());

If you really want to implement it using the approach you've started already, you need to add the relevant property as state. I recommend you cache a Type rather than a String.

public with sharing class MyBatchScheduler implements Scheduleable
{
    final Type batchType;
    final Integer batchSize;
    public MyBatchScheduler(Type batchType) { this(batchType, 2000); }
    public MyBatchScheduler(Type batchType, Integer batchSize)
    {
        this.batchType = batchType;
        this.batchSize = batchSize;
    }
    public void execute(ScheduleableContext context)
    {
        Database.Batchable<SObject> batch =
            (Database.Batchable<SObject>)batchType.newInstance();
        Database.executeBatch(batch, batchSize);
    }
}
added 398 characters in body
Source Link
Adrian Larson
  • 151.4k
  • 38
  • 247
  • 431

I highly recommend you just roll scheduling functionality into your batches and strip out all the cron information from your class. IMO that really belongs in the context where you schedule the job in the first place.

public class MyBatch implements Database.Batchable, Scheduleable
{
    public Integer batchSize = 200;
    public void execute(ScheduleableContext context)
    {
        Database.execute(this, batchSize);
    }
    // batchable implementation
}

You could even put this scheduling into an abstract class (e.g. ScheduleableBatch) and extend that instead of including this boilerplate in every batch you write. If you follow this pattern for both batch implementations, you would do something like:

system.schedule('Job A', '0 0 * * * ?', new ScheduleableBatchA());
system.schedule('Job B', '0 0 * * * ?', new ScheduleableBatchB());

I highly recommend you just roll scheduling functionality into your batches and strip out all the cron information from your class. IMO that really belongs in the context where you schedule the job in the first place.

public class MyBatch implements Database.Batchable, Scheduleable
{
    public Integer batchSize = 200;
    public void execute(ScheduleableContext context)
    {
        Database.execute(this, batchSize);
    }
    // batchable implementation
}

I highly recommend you just roll scheduling functionality into your batches and strip out all the cron information from your class. IMO that really belongs in the context where you schedule the job in the first place.

public class MyBatch implements Database.Batchable, Scheduleable
{
    public Integer batchSize = 200;
    public void execute(ScheduleableContext context)
    {
        Database.execute(this, batchSize);
    }
    // batchable implementation
}

You could even put this scheduling into an abstract class (e.g. ScheduleableBatch) and extend that instead of including this boilerplate in every batch you write. If you follow this pattern for both batch implementations, you would do something like:

system.schedule('Job A', '0 0 * * * ?', new ScheduleableBatchA());
system.schedule('Job B', '0 0 * * * ?', new ScheduleableBatchB());
Source Link
Adrian Larson
  • 151.4k
  • 38
  • 247
  • 431

I highly recommend you just roll scheduling functionality into your batches and strip out all the cron information from your class. IMO that really belongs in the context where you schedule the job in the first place.

public class MyBatch implements Database.Batchable, Scheduleable
{
    public Integer batchSize = 200;
    public void execute(ScheduleableContext context)
    {
        Database.execute(this, batchSize);
    }
    // batchable implementation
}