6
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??

2
10

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.

0

Static values are available per transaction, you need something for whole operation. As @Bachovski posted, just remove static from variable declaration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.