0

I have a schedulable class, that call a batch, like that :

global class batchSchedulable implements Schedulable {
  global void execute(SchedulableContext sc) {
      myBatch b = new myBatch();
      String sch = '0 0 * * * ? ';
      System.schedule('myBatch', sch, b);
  }
}

The batch myBatch work well when I run it with the developper console -> anonymous window.

But I have to run the batch myBatch every hour of every day. So I have to use a schedulable class that call it, right ?

But when I launch this schedulable class in the anonymous window, my batch is not working as expeted (the pupose of the batch is to update an object, and in this case no update happens)

  • Problem seems to be with batch. Please share your batch – Samir Jun 8 '17 at 14:19
1

Once the schedule is already running, you execute the batch, not schedule it:

public void execute(SchedulableContext context)
{
    Database.executeBatch(new MyBatch());
}

If you want a simple schedule, the UI would be the best way to actually queue up this job. But you can also use a script via Execute Anonymous, which would look something like:

system.schedule('Job Name', 'CRON', new MySchedulable());

One common pattern is to roll the scheduling and batch in the same class:

public class MyBatch implements Schedulable, Database.Batchable<SObject>
{
    public void execute(SchedulableContext context)
    {
        Database.executeBatch(this);
    }
    public Database.QuerLocator start(Database.BatchableContext context)
    {
        // implementation
    }
    public void execute(Database.BatchableContext context, List<SObject> scope)
    {
        // implementation
    }
    public void finish(Database.BatchableContext context)
    {
        // implementation
    }
}
  • How can I do for the schedule to be running ? – dou Jun 8 '17 at 14:56
  • @dou Just run System.schedule(...); once in anonymous Apex to set it up, or you can schedule it through the UI if you go into the class itself and click the button to schedule the job – Brian Miller Apr 8 at 7:05
0

Your scheduler class should execute batch and you have to schedule this class using schedule manager in org

or you can execute below code to schedule from developer console.

      String sch = '0 0 * * * ? ';
      System.schedule('myBatch', sch, new batchSchedulable ());

Scheduler class

global class batchSchedulable implements Schedulable {
      global void execute(SchedulableContext sc) {
          myBatch b = new myBatch();
          Database.executeBatch(b);
      }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.