Learn Batch Apex in Salesforce Like I am 10.
Can Learning Be More Fun?
Hello Friends, Lets Learn Batch Apex in Salesforce in Fun Way. With Help of Laundry Example.
Let the Washing Machine Wash your Clothes while you βNetflix and Chilβ!!!
What will you learn?
- What is Batch Apex
- Syntax of Batch Apex
- Real Time Salesforce Example
- Database.Stateful
Letβs get started on the fun ride!!
Batch Apex
Letβs dive into learning Batch Apex by using simple example of βdoing laundry.β Wow!!
Imagine you have a pile of dirty clothes that need washing. Instead of throwing them all into the washing machine at once, you decide to sort them into smaller batches to make the process more manageable. Letβs see step by step how it will work.
1. Collect Clothes
You start by gathering all your laundry, which includes a large pile of clothes. Now, imagine if you tried to wash all of them together in one go then machine might not fit all the clothes due to size of washing machine is small and number of clothes are a lot. Machines might also overflow because of this.
To avoid such a situation, you need to split your laundry into smaller batches. This way, you can wash each batch separately, ensuring that your clothes get cleaned properly. You divide them clothes into manageable batches, making the laundry process much more efficient.
2. Processing in Batches:
As the washing machine begins its cycle, it works through the batch of clothes, cleaning them thoroughly. Meanwhile, you can focus on other tasks while the machine does its work. Like You can do βNetflix and Chillβ while your washing machine runs in background.
3. Completing the Batch
Once the cycle is complete, you transfer the clean clothes to the dryer. While the first batch is drying, you can start the next batch of laundry in the washing machine.
You continue this process of sorting, washing, drying, and folding until all your laundry is clean and neatly put away. Breaking up the task into smaller batches makes it more manageable and ensures that each load of laundry is properly cleaned and cared for.
Batch Job Execution
When it comes to writing a Batch class, we need to implement the Database.Batchable interface.
Database.Batchable interface contains three methods that must be implemented.
Start Method:
The “Start” method is like this initial step where you assess the total amount of laundry you have and decide how to split it into manageable batches.
This start method returns a QueryLocator that defines the initial set of records to be processed. In our laundry example, it gathers all the clothes for processing.
Execute Method:
Now that you’ve identified the initial scope of your laundry, it’s time to start washing them in smaller batches. The “Execute” method is like this washing process, where each batch of clothes is processed separately.
Finish Method:
Once all the batches of laundry have been washed, it’s time to finish up and complete the process. The “Finish” method is like this final step, where any post-processing work is performed, such as folding and putting away the clothes.
This finish method defines any post-processing logic to be executed after all batches have been processed. In our laundry example, it would contain the code to fold and put away the clothes after washing them all.
Why we Use Batch Apex Example in Salesforce ?
Let’s simplify the explanation by taking Salesforce Example
Suppose you need to update a name field for every account to βsfdcamplified βin your Salesforce organization, but you have a large number of accounts, say 10,001. Doing this update all at once is impossible due to governor limits. Here comes the role of batch apex which handle large volume of data in batches.
But what is Batches?
If we have 10000 dirty clothes to wash and we want to process them in batches of 200, we can calculate the number of batches required as follows:
Total number of clothes: 10000
Batch size: 200.
Number of batches = Total number of clothes / Batch size
= 10000 / 200
= 50
In Salesforce, when we refer to ” batch,” we mean a group of records that are processed together. By default, each batch can handle up to 200 records. This means that when you execute a Batch Apex job, Salesforce divides the total number of records to be processed into smaller batches, with each batch containing a maximum of 200 records.
However, you can specify a custom batch size of minimum 1 and maximum of up to 2000 records if needed, allowing you to optimize the processing of large volumes of data more efficiently.
1. Start Method:
In the start() method, you define the query that will fetch the accounts to be updated. For example, you might use a query like βSELECT Id,Name FROM Account
2. Execute Method:
Next, the execute() method runs, but it only processes a subset of the accounts at a time, Within each execution of execute() Salesforce applies a separate set of system limits, so you have a fresh set of resources to work with. After processing one batch of accounts, the execute() method is called again for the next batch, and so on until all accounts are processed.
3. Finish Method:
Finally, the finish () method is called to wrap up any remaining tasks. This is where you might perform any additional actions, such as sending a status email to notify users that the batch job has been completed.
What is stateful in the batch apex?
Batch apex is an asynchronous process that is stateless by default. In Laundry example, each time you do a load of laundry, you start fresh – you gather your dirty clothes, sort them, wash them, fold them. Each laundry cycle is independent and doesn’t remember anything about the previous cycles.
Now, let’s say you want to keep track of the total number of clothes washed across multiple laundry cycles. In a stateless process, you wouldn’t have a way to remember the count from one cycle to the next because each cycle is treated as if it’s the first time.
However, if you make your laundry process stateful, it’s like having a notepad where you can jot down the total count of clothes after each cycle. With Database.Stateful in Batch Apex, you can maintain shared information across multiple executions of the batch job. So, in our laundry example, you could keep track of the total count of clothes washed across multiple batches.
For instance, if you washed 20 clothes in one batch and 30 in another, with a stateful process, you can keep track of the total count as 50 (20 + 30), even though each batch execution is independent.
Syntax
In the above example, LaundryBatch class implements both Database.Batchable<sObject> and Database.Stateful. It includes a member variable totalClothesWashed to store the total count of clothes washed across multiple batch executions. This variable retains its value between batch executions due to the Database.Stateful interface, allowing you to maintain shared information across batches.
I hope you enjoyed the article. If you have any suggestions or feedback.
Feel free to connect with me on:
Subscribe to my YouTube Channel
Follow my Blog β sfdcAmplified
We absolutely love your blog and find most of your post’s to be exactly what I’m looking for. Would you offer guest writers to write content to suit your needs? I wouldn’t mind composing a post or elaborating on a few of the subjects you write in relation to here. Again, awesome web site!