function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nivyajnivyaj 

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:

  1. 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?

  2. Or, is there a way to schedule your batch jobs? I’m looking into asyncApexJob and using that as a framework…

  3. Is there a better design?

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_
  1. 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.

  2. 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.

  3. Is there a better design?

    I'm unsure.

All Answers

Damien_Damien_
  1. 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.

  2. 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.

  3. Is there a better design?

    I'm unsure.

This was selected as the best answer
nivyajnivyaj

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

 

Damien_Damien_

The following three lines will schedule your class.  You need to execute them one time in execute anonymous or the Developer Console:

 

scheduledMerge m = new scheduledMerge();
String sch = '20 30 8 10 2 ?';
system.schedule('Merge Job', sch, m);

 

nivyajnivyaj

Thanks Damien_!