function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TheRealistTheRealist 

How to Schedule a batch Apex using system.schdule ?

How to Schedule a batch Apex using system.schdule with code not through UI and where to write it ,
Below is my sample code:

public class BatchLeadDelete implements database.Batchable<sobject>
{
    list<lead> llist=new list<lead>();
    public string query='select id,name,Phone from lead';
    public string flag='12345';
    
    public database.querylocator start(database.BatchableContext bc){
       return database.getQueryLocator(query); 
  }
    public void execute(database.BatchableContext bc,list<lead> le){
        for(lead l:le){
            if(l.Phone==flag)
            {
                llist.add(l);
            }
                
        }

      delete llist;             
    }
    public void finish(database.BatchableContext bc){
        
    }
 }

I called the above class with below code

BatchLeadDelete l=new BatchLeadDelete();
    database.executeBatch(l,50); 
Amit Chaudhary 8Amit Chaudhary 8
Please try to call your class like below 
database.executeBatch(new new BatchLeadDelete(),50);

Please try below schedulable class
global class MySchedulableClass implements Schedulable 
{
   global void execute(SchedulableContext ctx) 
   {
		database.executeBatch(new new BatchLeadDelete(),50);
   }   
}

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

Please let us know if this will help you
justin_sfdcjustin_sfdc

Hi there,

If you already have a schedular class written then you could either schedule it to run from the UI or you could run it via developer console.
To run it from dev console:
System.schedule('Schedule_Job_Name', '0 15 * * * ?', new schedulableClass());

This will schedule the job every 15th minute of each hour.

Let me know how often you want to schedule your schedular class.

Thanks!

TheRealistTheRealist
Thanks  @justin_sfdc and @Amit Chaudhary 8...

Hi justin_sfdc, how to schedule the job to run on
Mondays of every week at 10 hr 15th min
 
Amit Chaudhary 8Amit Chaudhary 8
Hi ,

Please follow below Steps:-
Step1 :- Create schduler class like below :-
global class MySchedulableClass implements Schedulable 
{
   global void execute(SchedulableContext ctx) 
   {
		database.executeBatch(new new BatchLeadDelete(),50);
   }   
}

Step 2 :- go to classes
Setup->Apex classes then click on Schedule Apex button 
User-added image

Step 3:- Then add date and time like below and select your scheduler class in Apex class Name

User-added image

Please mark this as solution if this will help you
justin_sfdcjustin_sfdc

Hi there,

Try this:

System.schedule('Scheduled Job 1', '0 15 22 ? * MON', new ScheduledClass());

Once you execute it, you can go to the Scheduled jobs to verify what time the time has been scheduled to run for. 

Hope this works! Let me know if it does not then I can try to run in my machine.
 

Thanks!