• #20877
     saloni gupta #672
    Forcetalks

    what is the use of Database.Stateful in batch class?

    why do we implement  Database.Stateful  in batch class?

    #20883
    Salesforce | Sudhir Sudhir #617
    Forcetalks

    I think you can do a little bit of searching inside Salesforce Apex Developer Guide to get this answer.

    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.

    #copied  #Salesforce_Apex_Developer_Guide

    Is there any specific answer you are seeking for? Please ask.

    #20884
    Salesforce | Aman Aman #4
    Forcetalks

    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.

    The following example summarizes a custom field total__c as the records are processed.

    global class SummarizeAccountTotal implements
    Database.Batchable<sObject>, Database.Stateful{
    global final String Query;
    global integer Summary;
    global SummarizeAccountTotal(String q){Query=q;
    Summary = 0;
    }
    global Database.QueryLocator start(Database.BatchableContext BC){
    return Database.getQueryLocator(query);
    }
    global void execute(
    Database.BatchableContext BC,
    List<sObject> scope){
    for(sObject s : scope){
    Summary = Integer.valueOf(s.get(‘total__c’))+Summary;
    }
    }
    global void finish(Database.BatchableContext BC){

    }
    }

     

    #20890
     saloni gupta #672
    Forcetalks

    thanks sudhir, Can you give any example for this scenario ?

    #20899
    Salesforce | Sudhir Sudhir #617
    Forcetalks

    Look at the example in the Aman’s response above.

    #35351
    Salesforce | Parul Parul #2
    Forcetalks

    Adding some more point:

    Hi

    Implementing database.stateful will maintain your state so the map that you are using in execute method will contain all the error messages after the completion of execute method.In finish method you will have aggregate of all errors that can be finally used .

    Thanks

    #35411
    Salesforce | shariq shariq #1
    Forcetalks

    Hi,

    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.

    If you’re ever not sure if you need it, simply ask yourself two questions: (1) “Does one execute method need data from a previous execute method?”, and (2) “Does the finish method need data from any previous execute method?” If the answer to both of these questions is no, then you do not need Database.Stateful. If the answer is yes, then you may want to use Database.Stateful. Keep in mind that there are alternative means to using Database.Stateful, such as storing data in a Custom Setting, which may offer better performance in some cases.

    Hope this helps.

    #51882
    Forcetalks

    scenario like:- for all the contacts for their birthday if you want to send email, that time you can use this to fetch all email id’s of contacts which are running in batches.

    #52361
     Divya #11
    Forcetalks

    Hi,

    Database.Stateful  maintains state across the 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.

Viewing 9 posts - 1 through 9 (of 9 total)

Please to reply to this topic.

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?