rspec/rules/S5385/apex/rule.adoc

44 lines
1.0 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
Salesforce provides a library of Aggregate functions like COUNT() which efficiently process the data records in the database and return the count of records which meet the SOQL Query criteria. Using size() function instead of COUNT() involves more processing and is less efficient.
This rule raises an issue when the result of an SOQL query is only used to call ``++size()++`` on it.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,apex]
2021-04-28 16:49:39 +02:00
----
public class fooClass{
public static fooMethod {
integer i = [SELECT Id FROM Case].size(); // Noncompliant
}
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,apex]
2021-04-28 16:49:39 +02:00
----
public class fooClass{
public static fooMethod {
integer i = [SELECT COUNT() FROM Case];
}
}
----
2021-04-28 16:49:39 +02:00
== See
* https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_count.htm[COUNT() and COUNT(fieldName)]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
endif::env-github,rspecator-view[]