Scheduling batch Apex

  • 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.

  • 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.

    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);'"

    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

    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 This allows you to store a reference to the Apex Job ID. Useful to programmatically check the status of the job later on.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM