There is a huge performance lag we faced when the code got deployed to Prod. In sandbox the same batch ran in few minutes but it prod it took some 2-3 hours. To overcome this we came up with a solution where the Batch will not get fired on every edit of Opportunity it will get fired on hourly basis with the newly inserted/Updated Opportunity Ids only.

Condition to run batch is; Opportunity should have a field populated. Field Name - Territory.

  1. Batch needs to run every hour.
  2. Pick up all the new Opportunities which got created/updated with Territory within that hour and run the batch job on those records only.

But the issue is what if some records fail in the previous Batch, I should also be able to include these in the next scheduled batch.

I should also not be picking the previously created Opportunities(for eg; old Opportunity records from year 2015, 2016, etc.) in the start method of the batch, so that it only runs for the Opportunities created within that timeframe?

The question is how will I design it.

My thoughts: Create a new CustomSetting and add two long text fields. In one text field I will keep on adding the ids of the opportunity through trigger logic which are recently getting updated or inserted based on my condition.

In another text field I will add Ids of failed Ids. But these needs to be cleared after every batch run.

How do I design this point?

  • What is your question exactly? Also including a brief version of your code would make it much easier to help you.
    – Adrian Larson
    Nov 22, 2017 at 16:29
  • You can create one isReady boolean field which you can add in your where clause of your SOQL query, so even if some records get failed in previous batch and if you add "WHERE isReady = True " in you where clause, when next batch job gets executed it will pickup those records. Say one record got failed in previous execution , make isReady = True, you can have retry count such that how many time you want to process that record, if it reaches the limit make isReady=false, so that batch will not pick that record again and again Nov 24, 2017 at 9:36
  • you need to update isReady Flag based on Record Record Processed/Falied, for Processed records update this Flag to "False" and also you need to update Re-Try Count for failed record, say you want to re-process records only 3 times and even after 3rd processing records get failed then make isReady =False for that record and your query will not pick that record. Nov 24, 2017 at 9:43

1 Answer 1


I would include a Checkbox Field on the Object that you update to True when your Batch is done. That way, if there are records that fail, they won't be updated, so they can be included in future runs.

For your last question, in your Start method, you should use a date filter so that it won't run on the older ones (as well as filtering the records that have the Checkbox = True)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .