batch apex

What is Batch Apex in Salesforce? How to Execute Batch Apex Class?

Batch Apex In Salesforce

  1. If you want to process a large number of records every day or within a certain time interval, you may encounter administrative restrictions.
  2. To resolve this governing limit issue, we will run the operation as an Asynchronous operation using batch apex.
  3. Batch Apex is exposed as an interface that must be implemented by developers. We can invoke at runtime batch jobs programmatically using apex.
  4. Batch Apex will break the large set of records into a number of batches with a small set of data and every batch will run independently from each other with a fresh set of governing limits.
  5. If you want any apex class to run like batch apex, that apex class has to implement an Interface “Database.Batchable”.
  6. Any apex class which implements Database.Batchable interface should define 3 methods:
    • Start
    • Execute
    • Finish

dont miss out iconDon't forget to check out: How To Use Database.Stateful Interface In Batch Apex In Salesforce

Start Method

  1. This method is called when the batch job starts and collects the data that needs to be operated on.
    Syntax: Database.Querylocator iteration start (Database.Batchablecontext bc) {}
  2. Database.Querylocator: When you use a simple query (SELECT...) to generate a range of records on which batch jobs should be run, use Database.Querylocator.
  3. If Querylocator objects are used, the total number of governors is limited.
  4. The number of records retrieved by the SOQL query is bypassed, and we can obtain up to 50 million records in the SOQL query.
  5. If you are using Iterable, all Governing limits will be still enforced.
  6. The start method will break the list of records into no. of batches and invoke the execute method on every batch.

Execute Method

  • This method will be invoked by the START Method on every batch of records.
  • This method will contain business logic that needs to be performed on the records fetched from the start method.
    Syntax: Global Void Execute(Database.Batchablecontext bc,List aacScope){}
  • List contains the list of records in the batch job on which this execute method is running.
  • Example: if the Start method fetched 1000 records and divided into 5 batches with 200 records in each batch, then execute method will be called on every batch separately (Execute method will be called 5 times)

Finish Method

  1. This method will be called after executing all the execute methods.
  2. This method is used for batch operations that send confirmation emails
    Syntax: global void finish(Database. Batchablecontext qc){}

dont miss out iconCheck out another amazing blog by Kirandeep here: Salesforce to Salesforce Integration Using SOAP API

Limits of Batch Apex

  1. At a time only 5 batch jobs will run.
  2. At a time 100 jobs can be added to flexQueue.
  3. In a batch Job, if anyone of the batch jobs fails, only that batch will fail and the rest of the batches will execute normally.
  4. If the finish method fails, only the finish method will fail and changes made by the execute method will be committed.
  5. We cannot call future methods from batch vertices.
  6. We can call the batch job from the completion method of another batch job.
  7. If you want to call web services from the batch apex, the apex class has to implement the “Database.allowscallouts” interface.

Reference: salesforcecodes, infallibletechie, webkul, success.salesforce, sfdcpoint

Popular Salesforce Blogs