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
.
- Batch needs to run every hour.
- 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?