You need to sign in to do that
Don't have an account?
Vinnie Bacon 14
How can I keep summary values for Accounts while processing Opportunities in a batch?
I'm trying to do something I think is pretty basic. I want to run a nightly batch that will summarize all of the Closed Won Opportunites by Account. (Forget the fact that these roll-up by default. I'm actually doing something different enough where this won't work.) We have tons of Opportunities and I need to do this in a nightly batch file. I'm comfortable with batches cranking away on Opportunities, but how would I keep a running tab BY ACCOUNT given that a specific batch might not have all of the Opportunities associated with that Account?
Do a nested soql on account in execute method, that way you will get all opportunities associated with that account.
All Answers
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 retain their values and are reset between transactions. Maintaining state is useful for counting or summarizing records as they’re processed. For example, suppose your job processed opportunity records. You could define a method in execute to aggregate totals of the opportunity amounts as they were processed.
If you don’t specify Database.Stateful, all static and instance member variables are set back to their original values.
Read more here https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
There has to be a basic way to do this. What I'm trying to do must apply to many examples. I'm finding things related to Aggregate Results and Batch processing. That seems to be what's needed but I'm nowhere near figuring it out just how to do it.
The start of each execute method would have you query this custom object for all the Accounts that will be processed in the current batch of Opportunities.
Do a nested soql on account in execute method, that way you will get all opportunities associated with that account.