• BT23
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies

Hello.  If I use Stateful in Batch Apex, will a List of contacts maintain its state across all the batch chunks?  I want to do a DML update with the List in the finish method.  The documentation is not clear if sObject variables/collections are maintained.  Thanks.

 

 

global class CalcFYTDBatch implements Database.Batchable<sObject>, Database.Stateful {
	Contact[] changedCons = new List<Contact>();

    //Execute the query.
    global database.querylocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope) {
        for(sObject s : scope) {
			Contact con = (Contact)s;
			tmpCon = new Contact(Id = con.Id);
			//various calculations
			changedCons.add(tmpCon);
			}
        }
    }
    
    global void finish(Database.BatchableContext BC) {
        if(!changedCons.isEmpty()) update changedCons;
    }

}

 

 

How do I get mail merge to work with windows 7