78 lines
2.6 KiB
HTML
78 lines
2.6 KiB
HTML
<div class="sect1">
|
|
<h2 id="_description">Description</h2>
|
|
<div class="sectionbody">
|
|
|
|
</div>
|
|
</div>
|
|
<div class="sect1">
|
|
<h2 id="_why_is_this_an_issue">Why is this an issue?</h2>
|
|
<div class="sectionbody">
|
|
<div class="paragraph">
|
|
<p>Moving the business logic out of Trigger functions and into dedicated "Trigger Handler" classes improves the application’s design:</p>
|
|
</div>
|
|
<div class="ulist">
|
|
<ul>
|
|
<li>
|
|
<p>the code is easier to test and maintain.</p>
|
|
</li>
|
|
<li>
|
|
<p>it helps avoiding some bugs such as trigger recursion.</p>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
<div class="paragraph">
|
|
<p>The Trigger functions should only dispatch calls to the corresponding Trigger Handler classes. See the links below for examples of Trigger Handler designs.</p>
|
|
</div>
|
|
<div class="paragraph">
|
|
<p>This rule raises an issue when a Trigger function contains one of the following syntax elements: loops, switch-case, try blocks, SOQL queries, DML queries, SOSL queries. The goal is to detect Trigger functions which have a complex logic. In practice method calls and if statements are enough to dispatch records for processing.</p>
|
|
</div>
|
|
<div class="sect2">
|
|
<h3 id="_noncompliant_code_example">Noncompliant code example</h3>
|
|
<div class="listingblock">
|
|
<div class="content">
|
|
<pre class="highlight"><code class="language-apex" data-lang="apex">trigger MyTrigger on Account(after insert, after update) { // Noncompliant. The trigger is processing records itself instead of using a Trigger Handler.
|
|
for(Account a : Trigger.New) {
|
|
// ...
|
|
}
|
|
}</code></pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sect1">
|
|
<h2 id="_resources">Resources</h2>
|
|
<div class="sectionbody">
|
|
<div class="ulist">
|
|
<ul>
|
|
<li>
|
|
<p><a href="https://web.archive.org/web/20210509202306/https://github.com/ChrisAldridge/Lightweight-Trigger-Framework">Lightweight Apex Trigger Framework</a></p>
|
|
</li>
|
|
<li>
|
|
<p><a href="https://web.archive.org/web/20230130071426/https://meltedwires.com/2013/06/05/trigger-pattern-for-tidy-streamlined-bulkified-triggers-revisited/">Trigger Pattern for Tidy, Streamlined, Bulkified Triggers Revisited</a></p>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
<hr>
|
|
</div>
|
|
</div>
|
|
<div class="sect1">
|
|
<h2 id="_implementation_specification">Implementation Specification</h2>
|
|
<div class="sectionbody">
|
|
<div class="paragraph">
|
|
<p>(visible only on this page)</p>
|
|
</div>
|
|
<div class="sect2">
|
|
<h3 id="_message">Message</h3>
|
|
<div class="paragraph">
|
|
<p>Move the business logic to a separate trigger handler class</p>
|
|
</div>
|
|
</div>
|
|
<div class="sect2">
|
|
<h3 id="_highlighting">Highlighting</h3>
|
|
<div class="paragraph">
|
|
<p>The Trigger function signature.</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div> |