0% found this document useful (0 votes)
11 views12 pages

Salesforce Apex Trigger Best Practices

This apex trigger best practices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views12 pages

Salesforce Apex Trigger Best Practices

This apex trigger best practices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

@deedev

Mastering Salesforce Apex

Apex Trigger
Best Practices
A Comprehensive Guide
@deedev
01

One Trigger Per Object


Why?
Ensures control over execution
order.
Prevents unpredictable
behavior.
Best Practice:
Consolidate all logic into a
single trigger per object.
Manage execution flow within
that trigger.
@deedev
02
Logic-less Triggers
Why?
Simplifies reading and
maintenance.
Enhances testability and
reusability.
Best Practice:
Delegate business logic to
handler classes.
Keep triggers focused on
context handling.
@deedev
03

Context-Specific Handlers
Why?
Clarifies which code runs in
each trigger context.
Improves organization and
readability.
Best Practice:
Create separate methods for
each context (before insert,
after update, etc.) in your
handler classes.
@deedev
04

Bulkify Your Code


Why?
Salesforce processes records in
batches of up to 200.
Prevents governor limit
exceptions.
Best Practice:
Operate on collections (lists,
maps).
Avoid assumptions about
single-record processing.
@deedev
05

No SOQL/DML in Loops
Why?
Exceeding governor limits
leads to runtime exceptions.
Impacts performance
negatively.
Best Practice:
Move SOQL queries and DML
operations outside of loops.
Query all necessary data
upfront.
@deedev
06

Collections & Efficient Loops


Why?
Optimizes performance and
resource usage.
Simplifies code structure.
Best Practice:
Use Maps and Sets for quick
lookups and to avoid
duplicates.
Streamline SOQL queries to
retrieve only needed data.
@deedev
07

Handling Large Data Sets


Why?
Avoids hitting governor limits
like heap size.
Ensures efficient data
processing.
Best Practice:
Use SOQL for loops to process
records in manageable
batches.
Limit the fields and records
retrieved.
@deedev
08

Proper Use of @future


Why?
Offloads long-running or callout
operations.
Runs processes
asynchronously.
Best Practice:
Bulkify @future methods to
handle collections.
Limit usage to necessary
scenarios.
@deedev
09
Avoid Hardcoding IDs
Why?
IDs can change between
environments.
Hardcoding leads to fragile,
non-portable code.
Best Practice:
Use Custom Settings or
Custom Metadata for
configurable data.
Query for records using
meaningful criteria.
@deedev
10
Additional Best Practices
Consistent Naming: Include the
object name (e.g.,
AccountTrigger).
Bulkify Helpers: Ensure helper
methods handle collections.
Simplify Triggers: Keep triggers
thin; delegate logic.
Efficient DML: Use collections
for DML operations.
Avoid Complex Logic in
Triggers: Keep business logic in
classes.
@deedev

Did you find


this useful ?
d b a ck !
r y an d s hare fee
ive it a t
G

You might also like