The importance of Salesforce trigger best practices cannot be overlooked. Apex Triggers are ideally event handlers. In other words, when a record related to the trigger is inserted, deleted, undeleted, or updated the Salesforce system will execute or “fire” that particular trigger event.
Salesforce will then execute a trigger in the following contexts: before-trigger, which are executed prior to a record being committed to a database; and after-trigger, which are events executed after committing a record to the database.
This Salesforce trigger tutorial will guide you through performance trigger standards.
Introducing The Salesforce Trigger Best Practices:1) One Trigger Per Object
According to this practice, one Apex Trigger is all that is needed for a particular object. Developing multiple Triggers for one object takes away your control over the order of execution as long as the Triggers are able to run in similar contexts.2) Logic-less Triggers
Writing methods in your Triggers means that, for test purposes, they cannot be exposed. You will also not be able to expose logic for re-use anywhere else within your organization.3) Context-Specific Handler Methods
These are to be created in Trigger handlers.4) Bulkify your Code
This is just to make sure the code efficiently handles multiple records at the same time.5) Avoid DML statements within FOR Loops or SOQL Queries
A single Apex request receives up to 100 SOQL queries before it exceeds the governor limit. Therefore, invoking this trigger by a batch of over 100 Account records would mean the governor limit would throw a runtime exception. This is one of the most important Salesforce trigger best practices.6) Using Streamlining Queries, Collections, and Efficient For Loops
Apex Collections can be used to efficiently query data and store it in memory. Using this in conjunction with streamlining SOQL queries could significantly aid in avoiding governor limits whilst writing efficient Apex code.
7) Querying Large Data Sets
SOQL queries can return 50, 000 records in a request. You may use a SOQL query for loop if returning a voluminous set of queries makes you exceed the heap limit.8) Use @future Appropriately
You should write your Apex code to handle multiple records concurrently, and this also applies to those that are annotated with the keyword “@future”.9) Avoid Hardcoding IDs
Avoid hardcoding IDs when deploying your Apex code between production and sandbox environments to allow dynamic identification of proper data. These Salesforce trigger examples can help you stick to Salesforce trigger best practices.
Before-Triggers are ideal for carrying out data validation, as they happen prior to committing a record’s changes to a database. They are also ideal for setting default values and performing extra logic/ calculations. Salesforce tutorials can be found over the internet, which is a rich source of information on Salesforce trigger best practices when using the Force.com platform.
After-Triggers, on the other hand, occur after committing a record to a database, with a record id. It is important to understand the context of a trigger (the ++Trigger++ object). In bulking mode triggers, all the examples involve looping through a series of records availed via the trigger.new property because all triggers will execute only in batch mode regardless of whether it is one or multiple records. So, always prepare your event triggers to efficiently handle a batch.