Using Batch Apex in Salesforce | The How-to Guide

Batch Apex

Batch Apex is used to run Large Jobs that would exceed processing limits. By using Batch apex, We can process thousands of records asynchronously in batches to stay within processing limits. 

The inner workings of Batch Apex are as follows. Let’s imagine you want to use Batch Apex to process millions of records. Each batch of records you process produces a single call to the batch class’ execution logic. The job is sent to the Apex job queue and executed as a discrete transaction each time you call a batch class. Two fantastic benefits of this feature include: 

  • It is simpler to make sure that your code stays under the governor execution limitations because every transaction starts with a new set of governor limits. 
  • No batch transactions are rolled back if one batch cannot be processed correctly. 

To write a batch class first we have to implement the Database.Batchable Interface and includes three methods:- 

  • Start Method 
  • Execute Method 
  • Finish Method 

Start Method

It is used to gather the data or objects that will be sent to an interface method to be processed. A Database or an error is returned by this function, which is called only once at the start of a Batch Apex task. The records or objects passed to the job may be contained in a QueryLocator object or an Iterable. 

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

Syntax:-  

public Database.QueryLocator start(Database.BatchableContext bc) { 
    return Database.getQueryLocator(‘ QUERY ’); 
    //collect the batches of records. 
}

Don’t forget to check out: All you Need to Know About Salesforce Apex Scheduler | The Ultimate Guide

Execute Method

The execute method is called for each batch of records passed to the method. Use execute method to do the required processing for each chunk or batch of data. There is no guarantee that batches of records will execute in the order the start method receives them. Default Batch Size is 200 Records. 

Syntax:-

public void execute(Database.BatchableContext bc, List<sobject> listname){ 
    //It will process each batch of records. 
}

Finish Method

The Finish method is called after all batches are processed. We can use this method to send Confirmation Emails or we can execute post-processing operations. A discrete transaction is regarded as each execution of a batch Apex job. If we do not write the finish method in a batch class, there will be a compilation error and the class won’t save at all.  

Syntax:-

public void finish(Database.BatchableContext bc) { 
    //execute post-processing operations. 
}

Invoking a Batch Class

To invoke a Batch Class, we will call Database.executeBatch with Instance : 

The syntax for Anonymous Window: 

MyFirstBatchClass myBatch = new MyFirstBatchClass(); 
Id classId = Database.executeBatch(myBatch);

dont miss out iconCheck out another amazing blog by Harsh here: All You Need to Know About Apex Testing in Salesforce | The Developer Guide

Using State in Batch Apex

Typically, Batch Apex is stateless. A discrete transaction is regarded as each execution of a batch Apex job. Using the default batch size, a batch Apex job with 1,000 records is regarded as five transactions with 200 records each. 

State can be maintained throughout all transactions if Database.Stateful is specified in the class definition. Only instance member variables’ values are carried over from transaction to transaction when using Database.Stateful. For counting or averaging records as they are processed, maintaining the state is useful. 

Example: 

public class bc implements Database.Batchable<sobject>, Database.Stateful { 
    // Code 
}

Limits in Batch Class

  • Apex supports a maximum of five batch jobs in the queue or running. 
  • The Database can get a maximum of 50 million records back to the object Database.QueryLocator. 
  • A maximum value of 2,000 can be used for the optional scope parameter of Database.executeBatch if the start method returns a QueryLocator. 
  • If the start method returns an Iterable, there is no upper limit on the value of the scope argument; nevertheless, if you choose a very high value, you might encounter other restrictions. 
  • Up to 10 callouts each can be implemented by the start, execute, and finish methods. 
  • 250,000 batch executions are allowed at most each day. 

 



Source link

Interesting Posts

Leave a Reply

Your email address will not be published. Required fields are marked *