[apex] Verify CognitiveComplexity for triggers

This commit is contained in:
Andreas Dangel 2024-09-06 18:42:03 +02:00
parent 6b0a2a1ffa
commit 9810bfc3cc
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3

View File

@ -222,4 +222,40 @@ public class Foo {
} ]]>
</code>
</test-code>
<test-code>
<description>Cognitive Complexity within triggers (#5138)</description>
<rule-property name="methodReportLevel">15</rule-property>
<expected-problems>1</expected-problems>
<expected-linenumbers>2</expected-linenumbers>
<expected-messages>
<message>The trigger 'AccountTrigger' has a cognitive complexity of 18, current threshold is 15</message>
</expected-messages>
<code><![CDATA[
// https://github.com/SalesforceLabs/Partner-Onboarding/blob/main/Partner%20Onboarding/triggers/AccountTrigger.trigger
trigger AccountTrigger on Account (after insert, after update) {
public List<Account> updatedAccounts = Trigger.new;
if(trigger.isAfter){
Map<Id,Account> mapOfOldValues = trigger.oldMap;
Map<Id,Id> accountWithOnboardingTemplateIDs = new Map<Id,Id>();
for(Account updated_Account : updatedAccounts) {
if(updated_Account.Onboarding_Plan__c != null)
{
if(mapOfOldValues == null){
accountWithOnboardingTemplateIDs.put(updated_Account.Id, updated_Account.Onboarding_Plan__c);
} else {
if(updated_Account.Onboarding_Plan__c != mapOfOldValues.get(updated_Account.Id).Onboarding_Plan__c)
accountWithOnboardingTemplateIDs.put(updated_Account.Id, updated_Account.Onboarding_Plan__c);
}
}
} //End For
if(accountWithOnboardingTemplateIDs.size() > 0){
OnboardingPlanService.updateContactsWithOndbPlan(accountWithOnboardingTemplateIDs);
}
}
}
]]></code>
</test-code>
</test-data>