You need to sign in to do that
Don't have an account?
Batch Apex conflicts and design?
I need help with a design issue and what happens in Batch Apex.
Setup
This is the scenario we have: We have a territory object, then when you update a single field needs to update a field on UPTO hundreds of thoursands of contacts. To do this, I am using Batch Apex. And invoking it on the territory record before it’s updated.
Question:
Say the user updates the territory from A to B, and clicks save. This causes a big batch of contacts to get updated and take a while Then, he changes B to C. Are we guaranteed that the final update on all impacted records will be C? How come?
Or, is there a way to schedule your batch jobs? I’m looking into asyncApexJob and using that as a framework…
Is there a better design?
Say the user updates the territory from A to B, and clicks save. This causes a big batch of contacts to get updated and take a while Then, he changes B to C. Are we guaranteed that the final update on all impacted records will be C? How come?
No. You do not have control over WHEN the records get updated. If you have 2 batch jobs running and updating the same object, 1 of 2 scenarios might happen. One batch might try to access a record while it is locked. There is a race condition to see which object gets reached first.
Or, is there a way to schedule your batch jobs? I’m looking into asyncApexJob and using that as a framework…
Yes, Use the Apex Scheduler to schedule batches to fire at a specific time.
Is there a better design?
I'm unsure.
All Answers
Say the user updates the territory from A to B, and clicks save. This causes a big batch of contacts to get updated and take a while Then, he changes B to C. Are we guaranteed that the final update on all impacted records will be C? How come?
No. You do not have control over WHEN the records get updated. If you have 2 batch jobs running and updating the same object, 1 of 2 scenarios might happen. One batch might try to access a record while it is locked. There is a race condition to see which object gets reached first.
Or, is there a way to schedule your batch jobs? I’m looking into asyncApexJob and using that as a framework…
Yes, Use the Apex Scheduler to schedule batches to fire at a specific time.
Is there a better design?
I'm unsure.
Thanks Damien_
I'm looking into Scheduled Apex to run the batches nightlly. Writing the class to implement scheduable seems easy enough which can create my batch job, and which will update my contacts.
QUESTION
I' dont' follow how to scheudle my batch job-- where is the Scheduable.execute() called? Where is my class instantiated?
I'm looking at this for guidance:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm
The following three lines will schedule your class. You need to execute them one time in execute anonymous or the Developer Console:
Thanks Damien_!