How can I schedule batch apex code?

501    Asked by dipesh_9001 in Salesforce , Asked on Apr 20, 2023

This is my batch apex code

global class batchAUpdate_based_on_stage implements Database.Batchable { 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 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>monitor>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()); } }

Where am I going wrong?

Just saving the class does not schedule batch apex code. 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); } } Then 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.



Your Answer

Interviews

Parent Categories