Making Use Of Salesforce Trigger Best Practices
Workflows are basically the sequence of administrative, industrial, or other practices through which a piece of work is passed through from initiation to completion. In simpler terms, a workflow can be described as the series of tasks employed to produce an outcome. When it comes to Salesforce workflows, there are times when the workflow will come across some limitations. Some of the common limitations include the workflow not being able to create or update separate objects or can’t do more than a simple field update. In such instances, a Salesforce trigger will be needed to ensure that the desired results are achieved.
So what are Salesforce trigger best practices? To have a good idea of what a trigger is, you should think of it as a workflow without limitations. With triggers, you can define entry actions and criteria, and can make these entries reference or do anything you want. With triggers, your imagination and coding skills are the only things that can limit the results you are looking for.
However, when it comes to using Salesforce triggers, there are a couple of practices that you should consider using to get the kind of results you are looking for. This article will be discussing some Salesforce trigger best practices that you should use for the best results.
Salesforce Trigger Best Practices
- One Trigger Per Object
If you want to make the most of your trigger, it is important that you consider using one trigger for a single object. If you create multiple triggers for one object, you will have no way of controlling how orders are executed if the triggers run in or on the same contexts.
- Logic-Less Triggers
Another best practice when working with triggers is ensuring that all your triggers are logic-less. What this means is that you need to ensure that the role of your trigger is only to delegate the responsibilities of the logic to another handler class. If you are going to be writing methods in your trigger, these methods cannot be exposed or used for test purposes. At the same time, you cannot expose logic to be used again anywhere else within your org’s workflow.
- Bulkify Your Code
Bulkfying codes simply refer to the process of ensuring that the code created properly handles more than a single record at any given time.
- Use Context Specific Handler Techniques
Another best practice is ensuring that you create or use context specific handler techniques within the trigger handlers. This way, if you were to implement new logics that continue to run even after an update, all you would need to do is to add some simple routing logic within the trigger itself to ensure that right handler technique is invoked.
- Query Large Data Sets
The set number of record that a SOQL query can return after a request is made is capped at 50,000. If your SOQL returns large query sets, causing you to exceed the set heap limit, then you should consider using a SOQL query instead. This query can process multiple record batches by using internal calls to query or queryMore.
The above are some of the most commonly-used Salesforce trigger best practices employed when it comes to Salesforce triggering.