You need to sign in to do that
Don't have an account?

How to use Database.Stateful in batch Apex?
Dear All,
The problem I am facing is, I have batch apex code updating two fields in Opportunity line Item(OLI). While I run the code for complete batch the values in the field are updating correctly. However, If I spilt the batch apex in two or many, I am facing the issue of the last batch value getting updated in Opportunity line item instead of complete one.
for ex:- Consider 3 record of value 50 each, I am expecting the field in OLI to be updated by 150 and it is updating rightly while I run the batch for Database.executeBatch(job,3); where as if I split the same as Database.executeBatch(job,2); then the field is getting updated by 50 instead of 150.
I read, Database.Stateful can be used to maintain the state accross the batch, can someone help me with sample code for Database.stateful illustrating how to use the function.
Thanks in Advance!
Simple example - we want to add up the sum of all opportunities for each Account and store it in a field on Account called test_amount__c - and for some reason we can't use a rollup.
So we build a batch that receives all opportunities withn an Account and Amount, and keeps a running total for each account using an account map, and finally update the accounts in the final part of the batch:
You can call it with this code:
All Answers
While database.stateful may well be the route you need to go, it sounds like you are overwriting the value in the OLI rather than adding the value that your batch has calculated. Is that your intention?
Yes, Exactly. I am trying to add up the batch values calculated instead of overwriting the values. Please help me with a piece of sample code if you have any.
Thanks in Advance!
Regards,
SRA
Can you post your code?
Sorry Bob, won't be able to post the code...If you have any generic code to help out me, that would be great!
I'm afraid not. However, there's an example in the Apex Developer's Guide at:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm
about 2/3 of the way down.
Bob's right - the example from the docs below should work for you - but instead of just using an integer, you can use a Map to keep track of each opportunity and it's running total, then loop through the map in the final method and update the Opportunities...
Can you please illustrate me on using Map for the integer, as you suggested?
Simple example - we want to add up the sum of all opportunities for each Account and store it in a field on Account called test_amount__c - and for some reason we can't use a rollup.
So we build a batch that receives all opportunities withn an Account and Amount, and keeps a running total for each account using an account map, and finally update the accounts in the final part of the batch:
You can call it with this code:
I am running in a similar kind of situation, Any help will be greatly appreciated.
I have Managed object called BIG MACHINE QUOTES. the req is if the Status field on Big Machine Quotes contains Unison then the Parent Object Opportunity field Stage should be Closed Won. and this batch should be scheduled every 15 min .
Here is my code so far.
Please help me on this
Thanks
Adil
Hi Adhil,
If the problem, is that the code is not maintaining the state then use the Keyword 'Database.Stateful' while initiating the batch job.
Also, to maintain the the state across the batches, set the list, under global string query declaration.
List<Opportunity> opp = new List<Opportunity>();
This will not empty the list everytime. Hope this helps. Let me know if this is useful!
Cheers,
SRA
I simply want to keep track of the number of records processed so it may be included in the email at the end.
Why does recordCount not get updated, or if it is, why is it back to zero inside finish()?
Never mind. I knew my batches were failing on the upsert (my trigger calls an @future method), but it wouldn't have occurred to me the state wouldn't have been updated.
All good.