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

134 lines
3.6 KiB
Plaintext

== 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 transaction. This is part of Governor limits.
Limiting the number of queries executed is necessary, and SOQL queries which have no chance of returning a result should be avoided. This is why SOQL Query of the form ``++SELECT ... FROM ... WHERE ... in :filterValues++`` should first check that the Set/Map (in this example named "filterValues") is not empty.
This rule raises an issue when such an SOQL query is not enclosed in one of
* ``++if (!filtervalues.isEmpty())++``
* ``++if (!filtervalues.size() != 0)++``
* ``++if (!filtervalues.size() > 0)++``
=== Noncompliant code example
[source,apex]
----
public class fooClass{
Set<Id> setCasesId = new Set();
//Noncompliant - SOQL Query executed without checking whether the Set is Empty
for(Case c :[SELECT Id FROM Case WHERE Id IN :setCasesId]){
//something
}
}
public class fooClass{
Map<Id, String> mapCasesId = new Map();
//Noncompliant - SOQL Query executed without checking whether the Map is Empty
for(Case c :[SELECT Id FROM Case WHERE Id IN :mapCasesId.keyset()]){
// ...
}
}
public class fooClass{
Map<Id, String> mapCasesId = new Map();
//Noncompliant - SOQL Query executed without checking whether the Map is Empty or Set is Empty
for(Case c :[SELECT Id, Subject FROM Case WHERE Id IN :mapCasesId.keyset() OR Id IN :setCasesId]){
// ...
}
}
public class fooClass{
Map<Id, String> mapCasesId = new Map();
//Noncompliant - SOQL Query executed without checking whether the Map is Empty and Set is Empty
for(Case c :[SELECT Id, Subject FROM Case WHERE Id IN :mapCasesId.keyset() AND Id IN :setCasesId]){
// ...
}
}
----
=== Compliant solution
[source,apex]
----
public class fooClass{
Set<Id> setCasesId = new Set();
if(!setCasesId.isEmpty()){
for(Case c :[SELECT Id FROM Case WHERE Id IN :setCasesId]){
//something
}
}
// OR
if(!setCasesId.size() > 0){
for(Case c :[SELECT Id FROM Case WHERE Id IN :setCasesId]){
//something
}
}
// OR
if(!setCasesId.size() != 0){
for(Case c :[SELECT Id FROM Case WHERE Id IN :setCasesId]){
//something
}
}
}
public class fooClass{
Set<Id> setCasesId = new Set();
if(!mapCasesId.isEmpty()){
for(Case c :[SELECT Id FROM Case WHERE Id IN :mapCasesId.keyset()]){
//something
}
}
}
public class fooClass{
Map<Id, String> mapCasesId = new Map();
if((!setCasesId.isEmpty()) || (!mapCasesId.isEmpty())){
for(Case c :[SELECT Id, Subject FROM Case WHERE Id IN :mapCasesId.keyset() OR Id IN :setCasesId]){
//something
}
}
}
public class fooClass{
Map<Id, String> mapCasesId = new Map();
//Recommended - SOQL Query executed after checking that the Map is Not Empty or Set is Not Empty
if((!setCasesId.isEmpty()) && (!mapCasesId.isEmpty())){
for(Case c :[SELECT Id, Subject FROM Case WHERE Id IN :mapCasesId.keyset() AND Id IN :setCasesId]){
//something
}
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Check that the Set or Map is not empty before executing this query.
=== Highlighting
The SOQL query
endif::env-github,rspecator-view[]