how to schedule an batch class to run every one hour every day - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
aditya raoaditya rao 

how to schedule an batch class to run every one hour every day

HI Experts,

I have to schedule the batch class for everyhour and every day below is my schedulear class , can anyone help me please.

Scheduled_Batch cCon = newScheduled_Batch ();
     Database.ExecuteBatch(cCon,200);
Best Answer chosen by aditya rao
Om PrakashOm Prakash
Hi Aditya,
Is it resolved and you tested your functionality, or if any query let's discuss.
 

All Answers

aditya raoaditya rao
HI Ompraksh,

I am getting below error. which i highlighted

 Method does not exist or incorrect signature: void schedule(String, String,Scheduled_Batch) from the type System at line 6 column 13

Scheduled_Batch cCon = newScheduled_Batch ();
  System.schedule('Hourly', '0 0 * * * ?', new Scheduled_Batch ());
     Database.ExecuteBatch(cCon,200);
Om PrakashOm Prakash
Please check your class Scheduled_Batch implements Schedulable.
global class Scheduled_Batch implements Schedulable {
   global void execute(SchedulableContext SC) {
      
   }
}

 
aditya raoaditya rao
yes i have written like above and got above mentioned issue.
aditya raoaditya rao
global class Scheduled Implements Schedulable
{
    global void execute(SchedulableContext sc)
    { 
     
 
     Scheduled_Batch m = new Scheduled_Batch ();
     String sch = '0 55 * * * ?'; 
     String jobID = system.schedule('RunReport1', sch, m);
     
  
    }
    
}

 
Om PrakashOm Prakash
For troubleshooting, please create this sample class in your dev org or sandbox
global class ScheduledDemo implements Schedulable {
   global void execute(SchedulableContext SC) {
       System.debug('Demo-');
   }
}

Now schedule it from developer console
System.schedule('Hourly ScheduledDemo', '0 0 * * * ?', new ScheduledDemo ());


Is it working as expected?
 
aditya raoaditya rao
oh ok, so you mean to say , i should not write. below line in the schedular class ?

System.schedule('Hourly ScheduledDemo', '0 0 * * * ?', new ScheduledDemo ());

will it run everyday for everyone hour. Kindly confirm.
aditya raoaditya rao
when i executed like below, its worked fine but my question will it automatically run eveyrday everyhour

 
global class Scheduled Implements Schedulable
{
    global void execute(SchedulableContext sc)
    { 
     
 
     Scheduled_Batch m = new Scheduled_Batch ();
     String jobID = system.schedule('RunReport1', sch, m);
     
  
    }
    
}

Developer console

  System.schedule('Hourly', '0 0 * * * ?', new Schedule() );

 
Om PrakashOm Prakash
You earlier shared code above from class Scheduled.
in the execute method you are trying to schedule a class Scheduled_Batch. (This should be removed from execute method).

As I understand Scheduled_Batch is your batch class which is not implementing Schedulable.

Can you share the first line of Scheduled_Batch class
 
aditya raoaditya rao
hi prakash,

global class Scheduled_Batch implements Database.Batchable<sObject>
Om PrakashOm Prakash
Yes, your recent version of the class should work as expected.

go to Setup > Scheduled Jobs
You can verify "Next Scheduled Run time" from there.

I also suggest having a look at this document 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
Om PrakashOm Prakash
Got it.
Scheduled_Batch is batch class as expected that's why you have the above error message above when you tried to schedule a batch class that has not implemented a Schedulable interface.

Your recent modified class and the above reply should help you.




 
aditya raoaditya rao
you mean to say. below code will run everyday and everyhour, i can see a batch for next one hour.
 
global class Scheduled Implements Schedulable
{
    global void execute(SchedulableContext sc)
    { 
     
 
     Scheduled_Batch m = new Scheduled_Batch ();
     String jobID = system.schedule('RunReport1', sch, m);
     
  
    }
    
}

Developer console

  System.schedule('Hourly', '0 0 * * * ?', new Schedule() );

 
Om PrakashOm Prakash
Yes
 
aditya raoaditya rao
ok, thanks last one more doubt,

can i give batch as 15k, only its should be only 2000 records at a time. Kindly let me know please.
Om PrakashOm Prakash
For example, a batch Apex job that contains 1,000 records and is executed without the optional scope parameter is considered five transactions of 200 records each.

You can define the scope max 2,000
Details here:
If the start method of the batch class returns a QueryLocator, the optional scope parameter of Database.executeBatch can have a maximum value of 2,000. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 2,000 records.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
Om PrakashOm Prakash
Hi Aditya,
Is it resolved and you tested your functionality, or if any query let's discuss.
 
This was selected as the best answer
John SchneiderJohn Schneider
An alternative method for scheduling is using the amazing, Mass Action Scheduler (https://douglascayers-org.github.io/sfdx-mass-action-scheduler/).