Batch Apex

            

     What is Batch Apex?

     Batch Apex is asynchronous execution of Apex code, specially designed for processing the     large number of records and has greater flexibility in governor limits than the synchronous code.

  It allows you to define a job that can be divided into manageable chunks, where each chunk can be processed separately.

For example, if you want to make a field update of all records in any object which is having more number of records, then governor limits restricts us to process that operation. Because in a single transaction we can only process 10,000 records.

In batch apex, it will fetch all records which you want to perform the field update and divide them into a list of 200 records & every 200 records operation is performed separately.


    when to use Batch Apex?

  • When you want to process large number of records on daily basis or even on specific time of interval then you can go for Batch Apex.

  • Also, when you want an operation to be asynchronous then you can implement the Batch Apex. Batch Apex is exposed as an interface that must be implemented by the developer. Batch jobs can be programmatically invoked at runtime using Apex. Batch Apex operates over small batches of records, covering your entire record set and breaking the processing down to manageable chunks of data.


  • What is Batchable Interface? To use Batch Apex, you must implement “Database.Batchable”. This interface has three methods. those are:

    1. Start
    2. Execute
    3. Finish

    Start method is automatically called at the beginning of the apex job. This method will collect record or objects on which the operation should be performed. These records are divided into subtasks and pass those to execute method.

  • Used to collect the records or objects to be passed to the interface method execute for processing. This method is called once at the beginning of a Batch Apex job and returns either a Database.QueryLocator object or an Iterable that contains the records or objects passed to the job.

  • With the QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed and you can query up to 50 million records. However, with an Iterable, the governor limit for the total number of records retrieved by SOQL queries is still enforced.


    execute
    Execute Method performs an operation which we want to perform on the records fetched from start method.

    Performs the actual processing for each chunk or “batch” of data passed to the method. The default batch size is 200 records. Batches of records are not guaranteed to execute in the order they are received from the start method.

    This method takes the following:

    1. A reference to the Database.BatchableContext object.
    2. 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

      Finish method executes after all batches are processed. Use this method to send confirmation email notifications

      Used to execute post-processing operations (for example, sending an email) and is called once after all batches are processed.

                                        batch class  syntax 
  1. global class MyBatchClass implements Database.Batchable<sObject> {
  2.     global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {
  3.         // collect the batches of records or objects to be passed to execute
  4.     }
  5.     global void execute(Database.BatchableContext bc, List<P> records){
  6.         // process each batch of records
  7.     }    
  8.     global void finish(Database.BatchableContext bc){
  9.         // execute any post-processing operations
  10.     }    
                    • }
                      • Invoking a Batch Class

                        To invoke a batch class, simply instantiate it and then call Database.executeBatch with the instance:

                        1
                        2
                        MyBatchClass myBatchObject = new MyBatchClass();
                        Id batchId = Database.executeBatch(myBatchObject);

                        You can also optionally pass a second scope parameter to specify the number of records that should be passed into the execute method for each batch. Pro tip: you might want to limit this batch size if you are running into governor limits.

                    • Every transaction starts with a new set of governor limits, making it easier to ensure that your code stays within the governor execution limits.
                    • If one batch fails to process successfully, all other successful batch transactions aren’t rolled back

                    Comments

                    Popular posts from this blog

                    Security model

                    EVENTS IN Lightning Web Components

                    Lightning web Components.