rspec/rules/S5383/apex/rule.adoc

57 lines
1.5 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
SOQL queries might return too many results and exceed the https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm#total_heap_size_limit_desc[maximum heap size]. To avoid this issue the best solution is to use SOQL for loops, i.e. for loops with inline an SOQL query. The loop will then chunk efficiently the query's results by calling "query" and "queryMore" methods.
This rule raises an issue when an SOQL query is executed and later a for loop iterates over its result.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,apex]
2021-04-28 16:49:39 +02:00
----
public class mySOQLLoop {
public static void myFunction() {
Task[] tasks = [Select Id, subject from Task];
for (Task task: tasks) {
// ...
}
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,apex]
2021-04-28 16:49:39 +02:00
----
public class mySOQLLoop {
public static void myFunction() {
for (Task task: [Select Id, subject from Task]) {
// ...
}
}
----
=== Exceptions
2021-04-28 16:49:39 +02:00
No issue will be raised when ``++LIMIT X++`` is used where ``++X++`` is a number less than or equal to 200. In this case adding a ``++for++`` loop will not improve the performance.
== Resources
2021-04-28 16:49:39 +02:00
* https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_SOQL.htm[SOQL For Loops]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Refactor this SOQL query and for loop into a single SOQL for loops
endif::env-github,rspecator-view[]