Skip to main content

Code reviews rule: Unbound SOQL statement

Written by David Martin
Updated this week

Unbound SOQL statement

Why is this an issue?

SOQL queries without a WHERE or LIMIT clause are completely unbound. If the result set exceeds the governor limit, a System.LimitException is thrown at runtime. Even when under the limit, unbound queries consume unnecessary heap space and slow down execution.

Examples

Example of incorrect code:

List<Account> accounts = [SELECT Id, Name FROM Account];

Example of correct code:

List<Account> accounts = [SELECT Id, Name FROM Account WHERE Id IN :accountIds];
List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 1000];

How can I fix violations?

Add a WHERE clause, a LIMIT clause, or both to constrain the query result set.

When should I disable this rule?

Dismiss violations on queries against objects with an inherently bounded number of records.

Resources

Did this answer your question?