Batch Apex in Salesforce

Batch Apex in Salesforce - Learn About the Advantages and Syntax

Batch class in Salesforce is utilized to run huge positions (think thousands or millions of records!) that would surpass typical handling limits. Utilizing Batch Apex, you can handle records non-concurrently in clusters (subsequently the name, "Batch Apex") to remain inside stage limits. On the off chance that you have a lot of records to process, for instance, information purging or filing, Batch Apex is most likely your best arrangement. 

This is the way Batch Apex works in the engine. Suppose you need to handle 1 million records utilizing Batch Apex. The execution rationale of the batch class is called once for each batch of records you are handling. Each time you summon a batch class, the occupation is put on the Apex work line and is executed as a discrete exchange. 

Advantages of Using Batch Apex:

  • Every transaction starts with a new set of governor limits and makes it easier to ensure that your code is stable within the governor execution limits. 
  • If one batch failed to process successfully then another successful batch of transactions isn’t rolled back.

dont miss out iconDon't forget to check out: Salesforce Apex to Flow - For Starters with a Very Simple Example

Batch Apex Syntax:

To write any new Batch Apex class, your class should execute the Database.Batchable interface and incorporate the accompanying three strategies: 

1. Start

Begin strategy is consequently called toward the start of the apex job. This technique will gather records or articles on which the activity ought to be performed. These records are separated into subtasks and passed those to execute strategy. 

Used to gather the records or has a problem with to be passed to the point of interaction technique execute for handling. This technique is called once toward the start of a Batch Apex work and returns either a Database.QueryLocator object or an Iterable that contains the records or items passed to the job. 

More often than not a QueryLocator gets the job done with a straightforward SOQL query to produce the extent of items in the cluster work. In any case, in the event that you want to accomplish something insane like circle through the consequences of an API call or pre-process records prior to being passed to the execute technique, you should look at the Custom Iterators connect in the Resources area. 

With the QueryLocator object, the lead representative breaking point for the all-out number of records recovered by SOQL query is avoided and you can inquiry up to 50 million records. In any case, with an Iterable, the lead representative breaking point for the absolute number of records recovered by SOQL query is as yet authorized. 

2. Execute

Execute Method plays out an activity which we need to perform on the records brought from the start method. 

Perform the real handling for each piece or "batch" of information passed to the method. The default batch size is 200 records. Batches of records are not ensured to execute in the request they are gotten from the beginning method. 

This method takes the following Point  as: 

  • To reference the Database.BatchableContext object. 
  • To list of sObjects,  as List<sObject>, or a list of parameterized types. If you are using the Database.QueryLocator and use the returned list. 

dont miss out iconCheck out another amazing blog by Narendra here: Learn the Use of Salesforce Lightning Web Component in Flow

3. Finish

The 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 one time after all the batches are processed. 

Syntax

global class MyBatchClass implements Database.Batchable<sObject> { 
    global (Database.QueryLocator | Iterable<sObject>)
    start(Database.BatchableContext bc)  
    // collect to the batches of records or objects to be passed to excute } 
    global void execute(Database.BatchableContext bc, List<P> records){ 
        // process all batch of records 
    }     
    global void finish(Database.BatchableContext bc){ 
        // execute any of the post-processing operations 
    }     
}

 

Responses

Popular Salesforce Blogs