𝑳𝒆𝒂𝒓𝒏 𝑩𝒂𝒕𝒄𝒉 𝑨𝒑𝒆𝒙 π‘³π’Šπ’Œπ’† 𝑰 π’‚π’Ž 10: 𝑳𝒆𝒕 𝒀𝒐𝒖𝒓 π‘Ύπ’‚π’”π’‰π’Šπ’π’ˆ π‘΄π’‚π’„π’‰π’Šπ’π’† 𝑫𝒐 𝒕𝒉𝒆 π‘³π’‚π’–π’π’…π’“π’š π‘Ύπ’‰π’Šπ’π’† 𝒀𝒐𝒖 π‘΅π’†π’•π’‡π’π’Šπ’™ 𝒂𝒏𝒅 π‘ͺπ’‰π’Šπ’π’!” πŸ˜€

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’!!!

Joseph Gomes - Senior Client Analyst ...

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!!

Laundry Service Cartoon With Coin ...

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.

Detergent manufacturers wish you'd ...

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.

Sorting laundry Stock Vector Images - Alamy

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.

Netflix and chill ...

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.

Kid Sorting Laundry Stock Illustrations ...

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.

A diagram of a program Description automatically generated

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.

A screenshot of a computer code Description automatically generated

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.

A screenshot of a computer code Description automatically generated

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.

A screenshot of a computer code Description automatically generated

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.

A diagram of a basket full of clothes Description automatically generated

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.

A screenshot of a computer program Description automatically generated

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.

A yellow and orange text Description automatically generated

I hope you enjoyed the article. If you have any suggestions or feedback.

Feel free to connect with me on:

LinkedIn

Subscribe to my YouTube Channel

Follow my Blog – sfdcAmplified

 

Did you enjoy this article?
Signup today and receive free updates straight in your inbox.
I agree to have my personal information transfered to MailChimp ( more information )
50% LikesVS
50% Dislikes

1 comment on “𝑳𝒆𝒂𝒓𝒏 𝑩𝒂𝒕𝒄𝒉 𝑨𝒑𝒆𝒙 π‘³π’Šπ’Œπ’† 𝑰 π’‚π’Ž 10: 𝑳𝒆𝒕 𝒀𝒐𝒖𝒓 π‘Ύπ’‚π’”π’‰π’Šπ’π’ˆ π‘΄π’‚π’„π’‰π’Šπ’π’† 𝑫𝒐 𝒕𝒉𝒆 π‘³π’‚π’–π’π’…π’“π’š π‘Ύπ’‰π’Šπ’π’† 𝒀𝒐𝒖 π‘΅π’†π’•π’‡π’π’Šπ’™ 𝒂𝒏𝒅 π‘ͺπ’‰π’Šπ’π’!” πŸ˜€

  1. Fitness

    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!

Comments are closed.