0

I have a problem executing a batch in some limit cases that i want to cover to avoid future concerns. The scenario is the following: My batch works on n(reasonable high number) records of the same SObject, initially i scheduled this batch to start with 2000 (the maximum number Salesforce provides for non-iterator BC) records per batch. Basically this job evaluate each record at the start and if it doesn't meet some conditions the record is discarded and so on. The limit case that lead me here is this, if inside the 2000 records permitted I have more than the 80% of records evaluated as true I received the "Too much query rows" (10k+1) alert, so we must keep the limit under 80% of 2000 to avoid this problem but my question is, can I retrieve some values around apex libraries in which I can check when the limit is near to the edge?

In particular the fact that the value 10k+1 is shown means that it is stored somewhere. Is it accessible?(if yes, the following request is unuseful) I cannot find that value in documentation, since counting any single DML is unthinkable is possible to calcutale dinamically the maximum number of records that can be processed avoiding the error but withound the value that I previously requested?

2
  • 1
    Are you encountering a SOQL limit or a DML limit? The standard SOQL limit is 50,000 (if not using a QueryLocator). The DML limit is 10,000 rows. Regardless, the easiest solution is to reduce your batch size to the maximum number of records your code can process in its worst-case scenario.
    – David Reed
    Nov 14, 2018 at 14:49
  • I specified that the limit regards DML (10k). Your solution is what I actually do but the solution I want calculate that number (dinamically based on one of the requested parameters in my question) so I don't have to check every day my dataset to know if the limit I set is correct. Nov 14, 2018 at 15:01

1 Answer 1

1

The Limits class gives you insight as to the current limits of a given transaction. Unfortunately, you can't choose to alter your batch size in the middle of an execution, which means you may need to be prepared to switch to a Queueable during execution (you're allowed 1, so use it sparingly). These limits are fixed as defined in governor limits. It is possible to make an intelligent choice based on if you'd exceed the limits, though most developers find it easier to simply select a lower batch size in hopes that 100% success is possible.

4
  • Thank you for the reply, unfortunately the dataset is not reliable and the today's limit can be wrong tomorrow. So the easiest solution does not reassure myself. Anyway i'll check on Limits class in search of something useful Nov 14, 2018 at 15:27
  • @J.Dillinger The limits have only ever increased over the years. Anything you write that works today will work tomorrow. The only difference is you might not be "as efficient" in the future.
    – sfdcfox
    Nov 14, 2018 at 15:34
  • Yes, I know but the hardcoded solution is what I currently use and I don't feel confident at all. Thanks all anyway for the help Nov 14, 2018 at 15:52
  • @J.Dillinger It's the same solution we all use for batchable. And we all struggle with. Queueable is a much better option if you get a choice, because you can always chain indefinitely to maximize performance.
    – sfdcfox
    Nov 14, 2018 at 15:53

You must log in to answer this question.

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