41 lines
1.2 KiB
Plaintext
41 lines
1.2 KiB
Plaintext
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
|
|
|
|
----
|
|
public class mySOQLLoop {
|
|
public static void myFunction() {
|
|
Task[] tasks = [Select Id, subject from Task];
|
|
for (Task task: tasks) {
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
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.
|
|
|
|
|
|
== See
|
|
|
|
* https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_SOQL.htm[SOQL For Loops]
|
|
|