Batch class is used when there is a need to process large amount of data(millions of records) that would exceed normal processing limits. This is the asynchronous way to process the records
and is specifically designed to process bulk data or records together and have a greater governor limit than the synchronous code.
Why need Batch class ?
Say, I have 500 opportunities and each one of them has 50,000 opportunity lineitems. Now total records that needs to be processed will be (500 * 50,000) = 2,50,00,000 records. How will I process these records as salesforce will not allow you to break governor limits.
Governor limits :
1. Total number of records retrieved by SOQLqueries : 50,000.
2. Total number of SOQLqueries issued : 200
3. Total number of DML statements issued : 150.
4. Total number of DML statements issued : 150.
5. Total number of records processed as a result of DML statements : 10,000.
Solution : The only answer is batch apex. Except this there is no other way to process this data. It will process up to 50 million records either you insert them ,update or delete them.
Now as per our example 2,50,00,000
Records to be processed : 2,50,00,000
Batch size : 10,00,000
No of batches : 2,50,00,000/10,00,000 = 25
Implementing Batch Apex
To implement batch apex we must need to implement Database.Batchable interface to tell the salesforce that it is a batch class which will process millions of records.
It has the following three methods that need to be implemented −
Start
Execute
Finish
Batch apex Syntax :
Implementation :
Start:
Start method is called automatically at the beginning of apex job and will be called only once. This method returns the Database.QueryLocator object that contains records to use in batch job or an iterable. You can retrieve up to 50 million records using QueryLocator.
Most of the time a QueryLocator does the trick with a simple SOQL query to generate the scope of objects in the batch job. But if you need to do something crazy like loop through the results of an API call or pre-process records before being passed to the execute method, you might want to check out the Custom Iterators link in the Resources section.
Execute:
Actual processing begins here. It will pick records from queryLocator process them and then pick another records as per batch size. This process continues until all the records get processed. You can set the batch size as per your need, if not set it will set default size of 200.
The Database.executeBatch method takes two parameters :
A reference to the Database.BatchableContext object.
A list of sObjects
Finish :
This method executes at last after all the methods finish their execution. It is used to execute post processing tasks ( for example send mail task).
One most important thing you can do in finish method is batch chaining. That I will explain in another blog.
Calling on Batch Apex
To make a call for batch class simply instantiate it and then call Database.executeBatch method:
BatchAccountUpdate obj = new BatchAccountUpdate ();
Id batchId = = obj.Database.executeBatch(obj);
You can also pass a scope parameter(optional) to process the number of records as per your need. This should be set based on logic if the logic is complex and you have set batch size to higher limit then off-course batch can get fail.
Limitations :
Up to five queued or active batch jobs are allowed for Apex.
A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed.
The start, execute, and finish methods can implement up to 10 callouts each.
The maximum number of batch executions is 250,000 per 24 hours.
CCNA Security: Entry-level certification focusing on foundational security concepts, threat management, and basic network security tasks.
CCNP Security: Professional-level certification covering advanced security skills, including secure access, VPNs, firewall protection, and security management on Cisco devices.
CCIE Security: Expert-level certification validating mastery in designing, implementing, and troubleshooting complex security solutions across various Cisco technologies.
These certifications validate expertise, enhance career opportunities, and are globally recognized in the network security field.
Join us at Networkers Home to start your journey in network security! We provide CCIE training, networking, cloud, and cybersecurity certification programs with 100% placement assistance to advance your career. Learn more at Networkers Home.
i want to become a salesforce developer so could you please tell me which websites for best practice purpose
my maill id is subbu2654@gmail.com
This blog is so cool. I am learning salesforce course online. This blogs helps me find out better understanding about salesforce training. Thank you for this blog!
Nice blog.
power bi training
devops online training
DevOps Training in Hyderabad .DevOps is a software engineering practice that aims at unifying software development and software operation. Spend more time on the code that matters and less on the tasks that slow your developers down. With tools like GitHub Actions and Packages, GitHub makes powerful CI/CD and automation part of your entire DevOps pipeline.
https://www.acte.in/devops-training-in-hyderabad