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
eswarsfeswarsf 

How to invoke a Apex class that implements Database.Batchable interface?

Hi,

I have written a Apex class hat implements Database.Batchable interface.What is the best way to call this class daily at specific time?

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You can invoke this from pretty much wherever you like.  The batch job gets queued for execution when the platform has resources.  I've used this in the following ways:

 

(1) From a button clicked by a user when they want to start some heavyweight processing

(2) From the system log when I needed to update a huge amount of accounts as part of deploying some code

(3) From a scheduled apex class when cleaning up data from the previous month on the first day of the month.

All Answers

bob_buzzardbob_buzzard

If you create another class that implements the schedulable interface, and creates/invokes the batch class, you will be able to schedule this either via the UI or other apex code.

 

There's an example of this in the Apex Developer's Guide at:

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_scheduler.htm|StartTopic=Content%2Fapex_scheduler.htm|SkinName=webhelp

eswarsfeswarsf

Hi bob,

Thanks for your reply.In this case i could have written my code that implements Schedulable interface.Kindly let me know in which scenarios we will choose to implement Database.Batchable interface and Schedulable interface?

bob_buzzardbob_buzzard

Generally speaking you'd use batchable when you need to process a large amount of records - i.e. more records than you can process in a single transaction.  For example, something which processes all accounts in your system.

 

You'd use schedulable when you want an apex class to be executed at a particular time.

eswarsfeswarsf

Thanks Bob for your reply.Generally from where we will invoke this apex class that implemented Database.Batchable interface?

bob_buzzardbob_buzzard

You can invoke this from pretty much wherever you like.  The batch job gets queued for execution when the platform has resources.  I've used this in the following ways:

 

(1) From a button clicked by a user when they want to start some heavyweight processing

(2) From the system log when I needed to update a huge amount of accounts as part of deploying some code

(3) From a scheduled apex class when cleaning up data from the previous month on the first day of the month.

This was selected as the best answer
eswarsfeswarsf

Thanks Bob.