15

Hi this is my batch apex code

global class batchAUpdate_based_on_stage implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Name,child_stage__c FROM Child__c ';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Child__c> scope)
    {
        for (child__c child : scope){

         if(child.child_stage__c == '25%'){
             child.child_stage__c = '100%';
         }
          update child;

        }
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}

And this is my scheduler class when i save it i'm not getting in the scheduled job list, i mean at Setup>moniter>scheduled job

global class Scheduler_class implements Schedulable{
    global void execute (SchedulableContext SC){
         batchAUpdate_based_on_stage b1 = new  batchAUpdate_based_on_stage();
       database.executeBatch(b1);
      // string sch = '0 0 0 1 4 ?';
       string sch = '0 1 1 * * ?';
       system.schedule ('Batch', sch, new Scheduler_class());
    }
}

can anyone tell where i'm going wrong.

2 Answers 2

26

Just saving the class does not schedule it. You still need to tell the DB to schedule it. I generally write a schedule function within my class so that I can easily call it in one line, that uses my static chron string within the class. Something like this should work

global class Scheduler_class implements Schedulable{

    public static String sched = '0 00 00 * * ?';  //Every Day at Midnight 

    global static String scheduleMe() {
        Scheduler_class SC = new Scheduler_class(); 
        return System.schedule('My batch Job', sched, SC);
    }

    global void execute(SchedulableContext sc) {

        batchAUpdate_based_on_stage b1 = new batchAUpdate_based_on_stage();
        ID batchprocessid = Database.executeBatch(b1,50);           
    }
}

The you just need to run Scheduler_class.scheduleMe(); from execute anonymous and it will be scheduled to run at your interval. This is important as this tells the DB to actually schedule it.

4
  • Thank you :) it's working great and it's very helpful and there was one mistake in 'return System.schedule('My batch Job', Scheduler_class, SC);' and i changed it to "'return System.schedule('My batch Job', sched, SC);'"
    – Sharat
    Commented Jan 17, 2014 at 12:51
  • Good catch. Yes, that was definitely a mistake. The issues of typing these up in text editors and not compiling them. :) Glad it worked for you Commented Jan 17, 2014 at 14:27
  • this code works fine can you please elaborate what this last line is doin ? "ID batchprocessid = Database.executeBatch(b1,50);" where and what is the use of passing batchprocessid ?
    – Sharat
    Commented Jan 18, 2014 at 1:02
  • 1
    @Sharat This allows you to store a reference to the Apex Job ID. Useful to programmatically check the status of the job later on.
    – user24634
    Commented Aug 2, 2016 at 9:16
9

The presence of the class alone will not cause it to make its way into any schedules.

I think you need to run System.schedule from execute anonymous (eg in Developer Console).

System.schedule('My First Schedule', '0 1 1 * * ?', new Scheduler_class())

You must log in to answer this question.

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