Activity Forums Salesforce® Discussions What are the different methods in Salesforce Batch Apex class?

  • shradha jain

    Member
    July 3, 2018 at 1:18 pm

    The different method of Batch Apex Class are:

    1. start method:
    It is used to collect the variables, records or objects to be passed to the method execute. It is called once at the beginning of a Batch Apex job. It returns either a Database.QueryLocator object or an Iterable that contains the variables, records or objects passed to the job.

    2. execute method:
    It performs the processing for each batch of data passed to the method. The default batch size is 200 records. The maximum size is 2000 records. Batches of records are not guaranteed to execute in the order they are received from the start method.

    3. finish method:
    It is used to execute post-processing operations. The post-processing operations could be sending an e-mail.

  • Parul

    Member
    September 14, 2018 at 9:20 am

    Start method
    The start method is called at the beginning of a batch Apex job.
    This method is Used to collect the records or objects to pass to the interface method execute.
    This method returns either a Database.QueryLocator object or an Iterable that contains the records or objects passed into the job.
    Query executed in the start method will return maximum 5,00,00000 records in a transaction.
    execute method:

    Execute method
    The execute method is called for each batch of records passed to the method.
    This method is used for do all the processing for data.
    This method takes the following: A reference of Database.BatchableContext object.
    A list of sObject records.
    Batches of records are not guaranteed to execute in the order they are received from the start method.
    finish method:

    Finish method
    The finish method is called after all batches are processed.
    This method is used to send confirmation emails or execute post-processing operations.
    This method takes only one argument a reference of Database.BatchableContext object.
    This method is called when all the batches are processed.

    Thanks

  • Avnish Yadav

    Member
    September 14, 2018 at 9:24 am

    Hey,

    The different methods in Salesforce Batch Apex class -

    1. start() method
    2. execute() method
    3. finish() method

    Here is the syntax of simple apex batch class:-
    `
    global class SimpleBatch implements Database.Batchable{

    global Database.QueryLocator start(Database.BatchableContext BC) {
    //
    }

    global void execute(Database.BatchableContext BC, List records) {
    //
    }

    global void finish(Database.BatchableContext BC){
    //
    }

    }
    `

    Thanks.

  • shariq

    Member
    September 14, 2018 at 12:02 pm

    Hi,

    The Database.Batchable interface contains three methods that must be implemented.

    Start method :

    global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {}

    To collect the records or objects to pass to the interface method execute, call the start method at the beginning of a batch Apex job. This method returns either a Database.QueryLocator object or an iterable that contains the records or objects passed to the job.

    execute method: 

    global void execute(Database.BatchableContext BC, list<P>){}

    To do the required processing for each chunk of data, use the execute method. This method is called for each batch of records that you pass to it.

    This method takes the following:A reference to the Database.BatchableContext object.
    A list of sObjects, such as List<sObject>, or a list of parameterized types. If you are using a Database.QueryLocator, use the returned list.

    finish method: 

    global void finish(Database.BatchableContext BC){}

    To send confirmation emails or execute post-processing operations, use the finish method. This method is called after all batches are processed.

    Hope this helps!

Log In to reply.

Popular Salesforce Blogs