global class batchForStateful implements Database.Batchable<Sobject>,Database.Stateful{
global static list<cust__c> soList=new list<cust__c>();
global database.querylocator start(Database.BatchableContext BC){}
global void execute(Database.BatchableContext BC, List<sObject> scope){
Util.staticMethod();
}
global void finish(Database.BatchableContext BC){
system.debug('--->asitm9'+soList.size());
//returning zero;
}
}
class Util{
static void staticMethod(){
if(batchForStateful.soList==null){
batchForStateful.soList=new list<cust__c>();
}
batchForStateful.soList.add(new cust__c() );
}
}
Debug statement should print size greater than zero, but why it is printing zero all the time??
Attribution to: Asitm9
Possible Suggestion/Solution #1
Static values are available per transaction, you need something for whole operation. As @Bachovski posted, just remove static from variable declaration.
Attribution to: Artur Kępczyński
Possible Suggestion/Solution #2
All your variables need to be instance variables - static ones reset for each execute method. Remove "static" from your variable declaration.
From the documentation:
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 and are reset between transactions.
Attribution to: Boris Bachovski
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34728