Skip to content

Commit b16bc59

Browse files
committed
SONARAPEX-6 add compliant code example
1 parent a12b03e commit b16bc59

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

rules/S5384/apex/rule.adoc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,35 @@ trigger MyTrigger on Account(after insert, after update) { // Noncompliant. The
2323
----
2424

2525

26+
=== Compliant solution
27+
28+
[source,apex]
29+
----
30+
public with sharing class AccountTriggerHandler {
31+
public static void handleAfterEvents(List<Account> newAccounts, System.TriggerOperation triggerEvent) {
32+
switch on triggerEvent {
33+
when AFTER_INSERT {
34+
processNewAccounts(newAccounts);
35+
}
36+
when AFTER_UPDATE {
37+
// ...
38+
}
39+
}
40+
}
41+
42+
private static void processNewAccounts(List<Account> accountList) {
43+
for (Account a : accountList) {
44+
// ...
45+
}
46+
}
47+
}
48+
49+
trigger AccountTrigger on Account(after insert, after update) { // Compliant: The trigger just delegates the work
50+
AccountTriggerHandler.handleAfterEvents(Trigger.new, Trigger.operationType);
51+
}
52+
----
53+
54+
2655
== Resources
2756

2857
* https://web.archive.org/web/20210509202306/https://github.com/ChrisAldridge/Lightweight-Trigger-Framework[Lightweight Apex Trigger Framework]

0 commit comments

Comments
 (0)