Tuesday, December 28, 2021

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.

Need of Batch Apex:

  • When you want to fetch thousands of records or fire DML on thousands of rows on objects it is very complex in salesforce and it does not allow you to operate on more than certain number of records which satisfies the Governor limits.
  • But for medium to large enterprises, it is essential to manage thousands of records every day. Adding/editing/deleting them when needed.
  • Salesforce has come up with a powerful concept called Batch Apex. Batch Apex allows you to handle more number of records and manipulate them by using a specific syntax.

Database.Batchable interface has the following three methods that need to be implemented.

start() :

It collects the records or objects to pass to the interface method execute(), call the start() at the beginning of a BatchApexJob. This method returns either a Database.QueryLocator object that contains the records passed to the job.

execute() :

To do the required processing of 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 a reference to the Database.BatchableContext object.

finish() :

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

Note: The order of execution of batches is not guaranteed.

While Implementing the Batch Programming, we have to follow the below steps.

	Step 1: Create a Global Class, which should be implemented by the
		"Database.Batchable" Interface.
			
		Syntax:
			Global Class <ClassName> Implements Database.Batchable<SObject>
			{
				// Write the Logic..
			}
			
	Step 2: Provide the Implementation for the Interface Methods.
		
		Syntax:
			Global Class <ClassName> Implements Database.Batchable<SObject>
			{
				Global Database.QueryLocator Start(Database.BatchableContext <refName>)
				{
					// Write the Start Method Logic..
				}
				
				Global void Execute(Database.BatchableContext <refName>, List<SObject>
																recordsToProcess)
				{
					// Write the Execute Method Logic..
				}
				
				Global void Finish(Database.BatchableContext <refName>)
				{			
					// Write the Finish Method Logic..
				}
			}

	Step 3: Invoke the Batch Class.
			
			Step 1: Create the Object of the Batch Class.
				Ex:
					<BatchClassName> <objectName> = new <BatchClassName>();
					
			Step 2: Invoke the Batch Class by using "Database.ExecuteBatch()" method.
				Ex:
					ID jobId = Database.ExecuteBatch(<batchClassObjectName>);
					
								(OR)
								
		ID jobId = Database.ExecuteBatch(<batchClassObjectName>, batchSize);
					
			Ways to Invoke the Batch Class:
			-------------------------------
			1. We can Invoke from Execute Anonymous Window.
			2. We can invoke from Another Batch Class.
			3. We can invoke from "Visualforce Page".
			4. We can Schedule the Batch Job.

	Step 4: Track the Status of the Batch Class.
			
			Case 1: Track the Status from the Setup Wizard.
					Setup --> Monitor --> Jobs --> Apex Jobs.
					
			Case 2: Get the Status through Programming by Querying from "AsyncApexJob"
					object.
					
					AsyncApexJob jobDetails = [Select id, status, totalJobItems,							jobItemsProcessed,numberOfErrors, CreatedBy.Email from AsyncApexJob
														Where id =: <batchJobId> ];


ES12 new Features