Apex Best Practices & Context Variables
These notes are based on the content on the Apex Step by Step website. It has variety of useful information about Apex and practice exercises with solutions.
Trigger Context Variables
IsInsert | " | IsUpdate | " | IsDelete | " | IsUndelete | |
---|---|---|---|---|---|---|---|
isBefore | isAfter | isBefore | isAfter | isBefore | isAfter | isAfter | |
new | Available | Available | Available | Available | Available | ||
old | Available | Available | Available | Available | |||
newMap | Available | Available | Available | Available | |||
oldMap | Available | Available | Available | Available |
Apex Best Practices
- Bulkify Your Code
- Bulkifying Apex means making sure that it properly handles more than one record at a time
- Avoid SOQL Queries or DML Statements inside FOR Loops
- This can trips governor limits that enforce a maximum number of SOQL queries/DML statements
- Bulkify Your Helper Methods
- Use Collections, Streamline Queries, and Write Efficient For Loops
- Use Apex Collections to efficiently query data and store it in memory. Thoughtfully combining appropriate collections with streamlined SOQL queries will help write efficient Apex code and avoid hitting governor limits.
- Use a Single Trigger per Object
- Salesforce is not able to enforce the order of execution for multiple triggers, so best practice is to collapse multiple triggers developed independently into a single trigger
- Querying Large Data Sets
- SOQL queries can only return 50,000 records at a time. If you are hitting heap limits, use a SOQL
for
loop to batch process records.
- SOQL queries can only return 50,000 records at a time. If you are hitting heap limits, use a SOQL
- Use the Limits Apex Methods to Avoid Hitting Governor Limits
- The
Limits
class lets you output debug messages for each governor limit, which can help triage particularly resource-intensive Apex.
- The
- Use
@future
Appropriately- Asynchronous methods have an independent set of higher governor limits, but it still has governor limits. And, no more than 10
@future
methods can be invoked within a single Apex transaction.
- Asynchronous methods have an independent set of higher governor limits, but it still has governor limits. And, no more than 10
- Write Test Methods to Verify Large Datasets
- An Apex trigger can be invoked by either a data operation from the user interface (generally single records) or by a data operation from an API (often many records). Use test methods that verify that all Apex code is properly designed to handle larger datasets without exceeding governor limits.
- Avoid Hardcoding IDs
- Record IDs can change between sandbox and production, so use logic and code that can dynamically identify the proper data to operate against without failing.
Use Cases for Asynchronous Apex
- Future Methods:
- Callouts to external Web services
- Operations which can run in their own thread
- To prevent the mixed DML error
- Batch Apex:
- When processing is required on many records
- Jobs that need larger query results
- Queueable Apex:
- Processing on non-primitive data types
- Chaining of jobs
- To start a long-running operation and get an ID for it
- Scheduled Apex:
- When a process needs to be run at some specific time