Print this page

Nonselective query fails on Apex trigger but succeeds in anonymous Apex and standalone query

Knowledge Article Number 000187995

Difference between nonselective query and anonymous Apex and standalone query


Anonymous Apex and standalone queries access the cache, so it's possible for these queries to actually succeed when they typically wouldn't if the records weren't cached. Triggers, on the other hand, don't access the cache, so they issue a new query for each and every call.

Why anonymous Apex or standalone queries succeed

Given the differences, trigger queries will encounter a non-selective query 100% of the time, while anonymous Apex may succeed if results are cached. Eventually, when the cache is cleared, anonymous Apex and standalone queries would also fail, but this discrepancy can be quite unclear at first.

promote demote