rspec/rules/S5380/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

108 lines
3.2 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

== Why is this an issue?
In order to protect shared resources, Salesforce enforces a maximum number of SOQL queries which can be executed inside a single https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_transaction.htm[transaction]. This is part of https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm[Governor limits]. If an SOQL query is nested inside a loop's body (For/While/Do-While) it might be executed more times than the Governor limit allows, making the code fail.
Thus it is a best practice to not have SOQL queries nested in the body of loops and instead retrieve all relevant data in a single query.
This rule raises an issue when it detects SOQL queries inside loops. This includes static SOQL queries (i.e. ``++[SELECT ...]++``) and calls to ``++Database.countQuery++``, ``++Database.getQueryLocator++`` and ``++Database.query++`` methods.
=== Noncompliant code example
[source,apex]
----
// Query in a for loop
public class MyClass {
public static void myFunction() {
for (Integer i = 0; i < 1000; i++) {
string firstName = 'Bob' + i;
Contact[] contacts = [ Select LastName From Contact Where FirstName = :firstName]; // Noncompliant
}
}
}
// Query in a while loop
public class MyClass {
public static void myFunction() {
Integer i = 0;
while ( i < 1000) {
string firstName = 'Bob' + i;
Contact[] contacts = [ Select LastName From Contact Where FirstName = :firstName]; // Noncompliant
i = i+1;
}
}
}
// Query in a do-while loop
public class MyClass {
public static void myFunction() {
Integer i = 0;
do {
string firstName = 'Bob' + i;
Contact[] contacts = [ Select LastName From Contact Where FirstName = :firstName]; // Noncompliant
i++;
} while (i < 1000);
}
}
----
=== Compliant solution
[source,apex]
----
public class MySOQLLoop {
public static void myFunction() {
List<string> names = new List<string>();
for (Integer i = 0; i < 10; i++) {
names.add('Bob' + i);
}
for (Contact contact: [Select LastName From Contact Where FirstName in :names]) {
// ...
}
}
}
----
=== Exceptions
No issue will be raised on SOQL querying https://help.salesforce.com/articleView?id=custommetadatatypes_overview.htm&type=5[custom metadata types] (i.e: type names ending with "__mdt")as https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm[Governor limits do not apply to them].
[source,apex]
----
public class MyClass {
public static void myFunction() {
for (Integer i = 0; i < 1000; i++) {
Contact[] contacts = [ Select myfield From MyCustomMetadataType__mdt]; // Ok
}
}
}
----
== Resources
* https://developer.salesforce.com/wiki/apex_code_best_practices[Apex Design Best Practices]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Move this SOQL query out of this loop.
=== Highlighting
SOQL query
endif::env-github,rspecator-view[]