How to know if I am about to hit the org governor salesforce limit class?

353    Asked by ClaudineTippins in Salesforce , Asked on May 9, 2023

I want to avoid hitting the org's governor limits but at the same time I need to know if I was about to hit them. Here's what I have so far:

[ SELECT convertCurrency(Price__c) FROM Object__c LIMIT :(Limits.getLimitQueryRows() - Limits.getQueryRows()) ];

So this will save me from hitting the limits but it will not tell me that I was going to hit them.

Basically the case I am talking about is when the query LIMIT is 50k and the records are 50k. If Limits.getQueryRows() is 50k it doesn't necessarily mean that a limit would have been hit. But if the records are 50001 I want to know this so I can display an error on the page for the user.

I was thinking about using Count() somehow but not sure how I can achieve what I want. I am also trying to incorporate the solution without making an additional query.

Answered by David EDWARDS

The way I see it, you have two options to avoid hitting or know when you are about to hit the org governor salesforce limit class.

Display your message whenever you end up with 0 remaining query rows available. Write a batch that will store the number of records that object contains in a Hierarchy Custom Setting.

Either way, it will probably simplify repeated references to add a helper method:
public static Integer getRemainingQueryRows() { return Limits.getLimitQueryRows() - Limits.getQueryRows(); }
For Option 1 your solution would simply look like:
someList = [ SELECT convertCurrency(Price__c) FROM MyObject__c LIMIT :getRemainingQueryRows() ]; isQueryLimitConsumed = (0 == getRemainingQueryRows());
For Option 2 your current implementation would change to look more like:
isQueryComplete = MySetting__c.MyObjectCount__c > getRemainingQueryRows(); someList = [/*query*/];

Your Answer

Interviews

Parent Categories