function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdcjoeysfdcjoey 

Database.Stateful in batch Apex

What is the use Database.Stateful in batch Apex ? Can some one explain with an example please.
Best Answer chosen by sfdcjoey
sfdcMonkey.comsfdcMonkey.com
hi sfdcjoey,
To maintain variable value inside the Batch class, Database.Stateful is used.
Sample Class:
global class Class_Name implements Database.Batchable<sobject>, Database.Stateful{
    Integer i = 0;
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocatory('SELECT Id, Name, Sequence_Number__c FROM Employee__c');
    }
    
    global void execute(Database.BatchableContext bc, List<Employee__c> listEmployee){
        for(Employee__c e : listEmp){
            e.Sequence_Number__c = i;
            i += 1;
        }
    }
    
    global void finish(Database.BatchableContext bc){
    }
}
here i value will be maintained even though execute method is called several times.

i hop it helps you
Please mark it best answer if it helps you so it make proper solution for others in future :)
thanks
 

All Answers

vishnu Rvishnu R
Navandeep_Kaur23Navandeep_Kaur23
Using Stateful Batch Apex
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.

global class SummarizeAccountTotal implements Database.Batchable<sObject>, Database.Stateful{
}
http://salesforceapexcodecorner.blogspot.in/2011/08/state-management-in-batch-apex-in.html

In Short if you need to send a mail to check number of record pass and failed in batch job counter in that case can you Stateful batch job.
If you want to create one counter and share/ use in each execute method use same.


Using Stateless Batch Apex
Batch Apex is stateless by default. That means for each execution of your execute method, you receive a fresh copy of your object. All fields of the class are initialized, static and instance.

global class SummarizeAccountTotal implements Database.Batchable<sObject>{
}

****************************************************

If you want to send the counter or share across the batch job then you need to use Database.stateful batch job.

Using Stateful Batch Apex
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.
http://www.infallibletechie.com/2014/06/how-to-maintain-variable-value-inside.html


global class Class_Name implements Database.Batchable<sobject>, Database.Stateful{
    Integer i = 0;
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocatory('SELECT Id, Name, Sequence_Number__c FROM Employee__c');
    }   
    global void execute(Database.BatchableContext bc, List<Employee__c> listEmployee){
        for(Employee__c e : listEmp){
            e.Sequence_Number__c = i;
            i += 1;
        }
    }
        global void finish(Database.BatchableContext bc){
    }
}

here i value will be maintained even though execute method is called several times.
sfdcMonkey.comsfdcMonkey.com
hi sfdcjoey,
To maintain variable value inside the Batch class, Database.Stateful is used.
Sample Class:
global class Class_Name implements Database.Batchable<sobject>, Database.Stateful{
    Integer i = 0;
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocatory('SELECT Id, Name, Sequence_Number__c FROM Employee__c');
    }
    
    global void execute(Database.BatchableContext bc, List<Employee__c> listEmployee){
        for(Employee__c e : listEmp){
            e.Sequence_Number__c = i;
            i += 1;
        }
    }
    
    global void finish(Database.BatchableContext bc){
    }
}
here i value will be maintained even though execute method is called several times.

i hop it helps you
Please mark it best answer if it helps you so it make proper solution for others in future :)
thanks
 
This was selected as the best answer