Merge pull request #3276 from jonathanwiesel:apexdocs

[apex] Update ApexCRUDViolation and OperationWithLimitsInLoop docs #3276
This commit is contained in:
Andreas Dangel 2021-06-18 15:00:05 +02:00
commit bb70866871
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
3 changed files with 31 additions and 2 deletions

View File

@ -56,6 +56,7 @@ This is a {{ site.pmd.release_type }} release.
### External Contributions
* [#3276](https://github.com/pmd/pmd/pull/3276): \[apex] Update ApexCRUDViolation and OperationWithLimitsInLoop docs - [Jonathan Wiesel](https://github.com/jonathanwiesel)
* [#3306](https://github.com/pmd/pmd/pull/3306): \[java] More than one logger rule test null pointer exception - [Arnaud Jeansen](https://github.com/ajeans)
* [#3317](https://github.com/pmd/pmd/pull/3317): \[java] Update UnnecessaryImport to recognize usage of imported types in javadoc's `@exception` tag - [Piotrek Żygieło](https://github.com/pzygielo)
* [#3319](https://github.com/pmd/pmd/pull/3319): \[apex] New AvoidDebugStatements rule to mitigate performance impact - [Jonathan Wiesel](https://github.com/jonathanwiesel)

View File

@ -144,7 +144,7 @@ public class Something {
class="net.sourceforge.pmd.lang.apex.rule.performance.OperationWithLimitsInLoopRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_apex_performance.html#operationwithlimitsinloop">
<description>
Database class methods, DML operations, SOQL queries, or SOSL queries within loops can cause governor limit exceptions. Instead, try to batch up the data into a list and invoke the operation once on that list of data outside the loop.
Database class methods, DML operations, SOQL queries, SOSL queries, Approval class methods, Email sending, async scheduling or queueing within loops can cause governor limit exceptions. Instead, try to batch up the data into a list and invoke the operation once on that list of data outside the loop.
</description>
<priority>3</priority>
<example>
@ -175,6 +175,32 @@ public class Something {
List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead];
}
}
public void messageInsideOfLoop() {
for (Integer i = 0; i < 10; i++) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
}
}
public void approvalInsideOfLoop(Account[] accs) {
for (Integer i = 0; i < 10; i++) {
Account acc = accs[i];
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setObjectId(acc.Id);
Approval.process(req);
Approval.lock(acc);
Approval.unlock(acc);
}
}
public void asyncInsideOfLoop() {
for (Integer i = 0; i < 10; i++) {
System.enqueueJob(new MyQueueable());
System.schedule('x', '0 0 0 1 1 ?', new MySchedule());
System.scheduleBatch(new MyBatch(), 'x', 1);
}
}
}
]]>
</example>

View File

@ -53,7 +53,9 @@ should be [suppressed](pmd_userdocs_suppressing_warnings.html).
<![CDATA[
public class Foo {
public Contact foo(String status, String ID) {
Contact c = [SELECT Status__c FROM Contact WHERE Id=:ID];
// validate you can actually query what you intend to retrieve
Contact c = [SELECT Status__c FROM Contact WHERE Id=:ID WITH SECURITY_ENFORCED];
// Make sure we can update the database before even trying
if (!Schema.sObjectType.Contact.fields.Name.isUpdateable()) {