I'm not even sure how to ask this question, but here goes:
I have Batch class that needs to maintain state. The class looks something like this:
global class myBatch implements Database.Batchable<sObject>, Database.Stateful {
global id[] AppliedChanges;
global database.Querylocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(accountsQuery);
}
global void execute(Database.BatchableContext bc, Account[] scope){
myBatch.doWork(scope);
}
global void finish(Database.BatchableContext bc){
//do something with AppliedChanges
}
public static void doWork(){
AppliedChanges = AppliedChanges == null ? new id[]{} : AppliedChanges;
//Do tons of work, eventually populate add to AppliedChanges
}
}
In doWork()
, I can debug AppledChanges
and see that it has values but, in the finish()
method, AppliedChanges
is always null.
The doWork()
method exists so I can use that logic elsewhere in Salesforce without having to run a batch.
How can I populate AppliedChanges
in the doWork()
method and then use it in finish()
?
public
though the doc examples usually use theglobal
modifier. developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… "useglobal
rarely, if at all"