Activity Forums Salesforce® Discussions What is Database.Stateful in Salesforce Batch Classes?

  • Yogesh

    Member
    August 13, 2019 at 12:54 pm

    Hello,

    The only time you need Database.Stateful is when the execute method modifies a class variable in a way meant to be used across multiple execute methods or in the finish method. The majority of batches you will ever write will not need Database.Stateful. It's important to know that using Database.Stateful will harm your batch's performance, because 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.

  • Deepak

    Member
    August 13, 2019 at 3:15 pm

    Using Stateful Batch Apex
    Batch Apex is stateless by default. That means for each execution of your executemethod, you receive a fresh copy of your object. All fields of the class are initialized, static and instance. If your batch process needs information that is shared across transactions, one approach is to make the Batch Apex class itself stateful by implementing the Stateful interface. This instructs Force.com to preserve the values of your static and instance variables between transactions.

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos