Loading

Non-Selective Query Error 'System.QueryException' in Salesforce Apex Trigger But Succeeds as Standalone Query

Publiseringsdato: Jun 19, 2026
Beskrivelse

Apex code may receive a "System.QueryException: Non-selective query against large object type" error when running a SOQL query in an Apex trigger context, even if the same query succeeds when run in the Developer Console.
Full error:

"System.QueryException: Non-selective query against large object type (more than 200000 rows). Consider an indexed filter or contact salesforce.com about custom indexing."

A query may still be non-selective even when a field is indexed if:

  • The filter value includes null (for example, binding with a list that contains null).
  • Data skew exists where the number of matching rows is very large (for example, filtering for a foreign key value that occurs many times).
Løsning

Why Non-Selective Queries Fail in Apex Trigger Context

By design, non-selective queries on large object types are not allowed within an Apex trigger context. This restriction prevents non-deterministic query execution time — which can result from full or partial table scans — from degrading the end user's experience when saving a record.


NOTE: If this restriction were not in place and a non-selective query ran in a trigger invoked on record save, the end user could be blocked waiting for the query to time out before receiving any UI feedback.
The restriction does not apply in the Developer Console or via SOAP/REST API, where a standard query timeout applies instead.

How to Optimize the Affected SOQL Query

Optimize the SOQL query to leverage an index. Refer to the Salesforce Query Search Optimization Developer Cheatsheet for guidance on writing selective, index-friendly queries. See Salesforce query search optimization developer cheatsheet for more details on how to optimize a SOQL query.

Knowledge-artikkelnummer

000386468

 
Laster
Salesforce Help | Article