rspec/rules/S5383/apex/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

57 lines
1.5 KiB
Plaintext

== Why is this an issue?
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
[source,apex]
----
public class mySOQLLoop {
public static void myFunction() {
Task[] tasks = [Select Id, subject from Task];
for (Task task: tasks) {
// ...
}
}
----
=== Compliant solution
[source,apex]
----
public class mySOQLLoop {
public static void myFunction() {
for (Task task: [Select Id, subject from Task]) {
// ...
}
}
----
=== Exceptions
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
* 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[]