Salesforce Best Practice: How to Use Trigger Context Variables

Learn how to efficiently use trigger context variables in Salesforce Apex to manage record changes in real time. Explore best practices for using context variables in your triggers for optimal performance and reliability.

SALESFORCE BEST PRACTICES

9/10/20243 min read

In Salesforce, Apex triggers are powerful tools that allow developers to perform operations before or after records are manipulated in the database. At the core of this functionality are trigger context variables, which provide critical information about the records being processed. By understanding how to use these variables effectively, you can write optimized and reliable triggers. In this post, we will explore the best practices for using trigger context variables in Salesforce.

What Are Trigger Context Variables?

Trigger context variables give developers access to important data during trigger execution, such as the records being inserted, updated, deleted, or undeleted. These variables allow developers to control logic flows based on whether the trigger is running before or after a DML operation, and whether it involves insert, update, or delete actions.

Common Trigger Context Variables:
  • Trigger.isInsert: Checks if the current operation is an insert.

  • Trigger.isUpdate: Checks if the current operation is an update.

  • Trigger.isDelete: Checks if the current operation is a delete.

  • Trigger.isBefore: Indicates if the trigger is running before the DML operation.

  • Trigger.isAfter: Indicates if the trigger is running after the DML operation.

  • Trigger.new: Provides a list of new records for insert or update operations.

  • Trigger.old: Provides a list of old records for update or delete operations.

  • Trigger.newMap: Provides a map of new records for insert or update operations.

  • Trigger.oldMap: Provides a map of old records for update or delete operations.

Best Practices for Using Trigger Context Variables

1. Use Specific Context Variables for Better Control

One of the best practices for writing efficient triggers is to use the appropriate context variable for the operation. Instead of checking all context variables in a single block, handle each operation separately.

Example:

2. Leverage Trigger.new and Trigger.old Properly

When working with insert or update operations, the Trigger.new variable holds the new versions of the records, while Trigger.old stores the original records before an update or delete operation.

Best Practice:

  • Use Trigger.new for insert or update operations to reference new field values.

  • Use Trigger.old for update or delete operations to compare old and new values.

Example:

3. Use Trigger.isBefore for Data Validation

If you need to perform data validation or modify record data before saving it to the database, use Trigger.isBefore. This ensures that your logic runs before the records are committed to the database.

Example:

4. Use Trigger.isAfter for Related Record Operations

Operations that involve updating related records or sending external notifications should be done after the record is saved to the database. Use Trigger.isAfter to handle these scenarios.

Example:

5. Avoid Hardcoding Logic Based on Operation Types

To ensure that your triggers are scalable and easy to maintain, avoid hardcoding logic based on specific DML operations or assumptions about the data. Instead, use context variables dynamically and apply the appropriate logic for each operation.

6. Use Maps for Efficiency with Trigger.oldMap and Trigger.newMap

For update and delete operations, use Trigger.oldMap and Trigger.newMap to reference the old and new records more efficiently, especially when performing comparisons or looking up records by ID.

Example:

Conclusion

Using trigger context variables effectively is key to writing efficient and maintainable triggers in Salesforce. By following best practices—such as using Trigger.new and Trigger.old properly, handling operations before and after the DML process, and avoiding hardcoding logic—you can ensure that your triggers run smoothly without hitting governor limits or performance issues.

FAQs

1. What are trigger context variables in Salesforce? Trigger context variables provide access to information about the records being processed during a trigger’s execution, such as whether the operation is an insert, update, or delete.
2. What is the difference between Trigger.new and Trigger.old? Trigger.new contains the new version of the records (after insert or update), while Trigger.old contains the original version of records (before update or delete).
3. When should I use Trigger.isBefore vs. Trigger.isAfter? Use Trigger.isBefore when you need to validate or modify data before committing it to the database. Use Trigger.isAfter for operations that occur after records are saved, such as updating related records.
4. How do I compare old and new values in a trigger? You can use Trigger.oldMap and Trigger.newMap to compare old and new field values, especially during update operations.
5. What is the purpose of System.addError() in triggers? System.addError() is used in before triggers to prevent records from being saved if they fail custom validation logic. It displays an error message to the user.