When should I use a database.stateful?

223    Asked by DavidPiper in Salesforce , Asked on Feb 8, 2023

I am writing a batch class which will get the records whose status picklist value is 'no response' and whose created date is before greater than or equal to 48 hrs and update the status to 'declined'.

Do I need to use Database.stateful here ?

global class SolicitaBatch implements Database.Batchable{
String query;
String email;
Id toUserId;
Id fromUserId;
global Database.QueryLocator start(Database.BatchableContext BC)
    {
        Datetime dtime = datetime.now().addhours(-48);
        String query = 'select id,createdDate, Status__c from Solicitation__C  where createdDate <= :dtime and Status__c = 'No Response' ';
        return Database.getQueryLocator(query);
    }
global void execute(Database.BatchableContext BC, List scope){
    List Sols = new List();
    for(Solicitation__C s : scope)
    {
        s.Status__c = 'ABCD';
    }
    update scope;

global void finish(Database.BatchableContext BC)
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] {email});
        mail.setReplyTo('batch@acme.com');
        mail.setSenderDisplayName('Batch Processing');
        mail.setSubject('Batch Process Completed');
        mail.setPlainTextBody('Batch Process has completed');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}
Answered by Ava Black

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 execution 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.


Your Answer

Interviews

Parent Categories