Dive into : Database.Stateful

Dive into : Database.Stateful

We know that in Salesforce we have a powerful way to execute things in bulk using Batch Apex . But seldom we arrive in a scenario where we need Database.Stateful .

Batch apex is an asynchronous process that is stateless by default . In simple words, stateless process can be understood in isolation. There is no stored knowledge of or reference to past transactions. Each transaction is made as if from scratch for the first time. This means that for each execution of your?execute?method, you receive a fresh copy of your object.

But if the batch process needs information that is shared across the transactions then we can make the Batch Apex?Stateful using Database.Stateful.

What exactly is stateful ?

Stateful process can be easily understood as they’re performed with the context of previous transactions and the current transaction may be affected by what happened during previous transactions.

In salesforce when we make the batch apex stateful then the class will be serialized at the end of each?execute?method to update its internal state. This extra serialization results in longer execution time.

Each execution of a batch Apex job is considered a?discrete transaction. For example, a batch Apex job that contains 1,000 records and is executed without the optional scope parameter is considered five transactions of 200 records each.

If you specify Database.Stateful in the class definition, you can maintain state across these transactions. When using Database.Stateful, only instance member variables retain their values between transactions. Static member variables don’t retain their values and are reset between transactions. Maintaining state is useful for counting or summarizing records as they’re processed. For example, suppose your job processed opportunity records. You could define a method in execute to aggregate totals of the opportunity amounts as they were processed.

If you don’t specify Database.Stateful, all static and instance member variables are set back to their original values.

Simple example given below:

No alt text provided for this image

In the above example

Case 1 : Without Stateful

Without database.stateful?the?count?value will be reset to zero for every batch

Case 2 : With Stateful

With database.stateful?the?count?value will not reset for every batch execute method.

要查看或添加评论,请登录

Deep B.的更多文章

  • Embracing SOLID Principles in Apex: A Guide for Salesforce Developers

    Embracing SOLID Principles in Apex: A Guide for Salesforce Developers

    Salesforce development often involves creating robust and scalable solutions to meet the dynamic needs of businesses…

    1 条评论
  • Iterable Batch Apex

    Iterable Batch Apex

    Let’s traverse through data! The Database.Batchable interface contains three methods that must be implemented.

  • Efficient SOQL queries

    Efficient SOQL queries

    If you’re doing development in an org with a Large Data Volume then you must be careful with SOQL For best performance,…

  • Notable Nuances in JavaScript- 01

    Notable Nuances in JavaScript- 01

    This short read highlights some features that may not be majorly used 1. Replace can accept a callback function The…

    2 条评论

社区洞察

其他会员也浏览了