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
MaheshReddyMaheshReddy 

How can write schedulable apex class in batch apex class

Hi ALL

     How can schedule batch class with in the batch apex class
Rahul KumarRahul Kumar (Salesforce Developers) 

Hi Mahesh,

Batch Apex in Salesforce

To use batch Apex, you must write an Apex class that implements the Salesforce-provided interface Database.Batchable, and then invoke the class programmatically.

Start method

          The start method is called at the beginning of a batch Apex job. Use the start method to collect the records or objects to be passed to the interface method execute.
Syntax: global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {}

This method returns either a Database.QueryLocator object or an iterable that contains the records or objects being passed into the job.

Execute Method

          The execute method is called for each batch of records passed to the method. Use this method to do all required processing for each chunk of data.
Syntax: global void execute(Database.BatchableContext BC, list<P>){}

This method takes the following:
o    A reference to the Database.BatchableContext object.
o    A list of s Objects, such as List<sObject>, or a list of parameterized types. If you are using a Database.QueryLocator, the returned list should be used.
Batches of records execute in the order they are received from the start method.

Finish Method
Syntax: global void finish(Database.BatchableContext BC){}


 The finish method is called after all batches are processed. Use this method to send confirmation emails or execute post-processing operations.
Each execution of a batch Apex job is considered a discrete transaction. For example, a batch Apex job that contains 1,000 records and is executed without the optional scope parameter from Database.executeBatch is considered five transactions of 200 records each.

The Apex governor limits are reset for each transaction. If the first transaction succeeds but the second fails, the database updates made in the first transaction are not rolled back.

Example of Batch Apex Class: 
 
Batch Schedule Class
 
global class batchContactUpdate implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id, FirstName,LastName FROM Contact';
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, List<Contact> scope)
    {
         for(Contact a : scope)
         {
             a.FirstName=a.FirstName+'FirstName is Updated';
             a.LastName = a.LastName +'LastName is updated';          
         }
         update scope;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}

Schedule Class:
 
global class BatchScheduleUpdate implements Schedulable
{
    global void execute(SchedulableContext sc)
    {
        // Implement any logic to be scheduled
       
        // We now call the batch class to be scheduled
        BatchContactUpdate b = new BatchContactUpdate ();
       
        //Parameters of ExecuteBatch(context,BatchSize)
        database.executebatch(b,200);
    }
   
}

Schedule from Developer console:
BatchScheduleUpdate batchSch=new BatchScheduleUpdate();
String sch='0 5 2 * * ?';
//System.schedule(String jobName, String cronExp, APEX_OBJECT schedulable);
System.schedule('Batch Schedule', sch , batchSch);

Thanks
Rahul Kumar
GulshanRajGulshanRaj
Hi MaheshReddy,

You can implement both Database.Batchable and Schedulable in same class like this:
global class ScheduleAndBatch implements Database.Batchable<sObject>, Schedulable {
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
      
    }
    global void execute(Database.BatchableContext BC, List<sObject> lstSobects) {
      
    }
    global void finish(Database.BatchableContext BC) {
          
    }
    global void execute(SchedulableContext sc) {
        Type t = Type.forName('ScheduleAndBatch');
        Database.Batchable< sObject > batchClass = ((Database.Batchable< sObject >) t.newInstance());
        Database.executeBatch(batchClass, this.scope);
    }
}

Thanks & Regards
Gulshan Raj
LinkedIn (https://www.linkedin.com/in/gulshan-raj-a26b0640/)
Twitter (https://twitter.com/gulshan_bittoo)