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
Platy ITPlaty IT 

Non-selective query- can an Apex controller extension/controller hit this error?

I've hit the non-selective query error in triggers for organizations with a lot of data and know how to deal with it there, but I'm wondering if this same error can occur in Apex code fired by a Visualforce controller/extension as well?  The Salesforce article related to this (http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_VLSQ.htm) seems to limit it to triggers, but does anyone know for sure if that's the case?

 

Context- I'm developing a managed package that includes a VF page that will query records and I'm wondering if I should be factoring this into the design and help files.

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Yes, it is possible to trigger non-selective query errors in the API, through Visualforce, and, of course, in triggers. It occurs any time you exceed the limits for triggering this error. Of course, it's hard to test for this in a normal-sized org, since you need at least 100,000 records available to query from (200MB of database storage, far more than a DE can hold). However, the system may choose to honor the query in some scenarios. For example, it is usually possible for a System Administrator to non-selectively filter against very large objects through the API, even though a normal user would simply receive an error instead. In general, you should always consider using as concise filters as possible to avoid the non-selective query error.

All Answers

sfdcfoxsfdcfox

Yes, it is possible to trigger non-selective query errors in the API, through Visualforce, and, of course, in triggers. It occurs any time you exceed the limits for triggering this error. Of course, it's hard to test for this in a normal-sized org, since you need at least 100,000 records available to query from (200MB of database storage, far more than a DE can hold). However, the system may choose to honor the query in some scenarios. For example, it is usually possible for a System Administrator to non-selectively filter against very large objects through the API, even though a normal user would simply receive an error instead. In general, you should always consider using as concise filters as possible to avoid the non-selective query error.

This was selected as the best answer
Platy ITPlaty IT

Thanks sfdcfox.  What I'm building allows users to dynamically filter for records based on fields they select, so it's entirely dependent on their data and how they choose to filter for it.  So I'm just building in catches and trying to include some explanations in the help file to explain the limits.

 

Any idea if sharing rules on data apply towards selective queries?  So, if an object has more than 100,000 records but the user that fires the query (in a Visualforce controller class with sharing enabled) only has access to 1,000 records, does that mean they'll be below the threshhold for hitting a non-selective error?  

sfdcfoxsfdcfox

That's correct. The entire query is filtered against the user's access to records, so a non-selective query will still be selective if they only have access to a handful of records. Just remember the limit for non-selective queries is 10% up to one million records, and 5% beyond the first million up to a maximum of 330,000 records. If they fall below this threshold as a total number of records they have access to, the user shouldn't experience this error normally.

Platy ITPlaty IT

Thanks sfdcfox, that certainly lowers the likelihood of it being hit anyway.  I will document accordingly.