Don't be Trigger Happy

Triggers can be very useful when modifying a record before or after an insert, update, deletion, update, merge or undelete action.  But many times, there are other ways to download triggerautomate the process.  As a programmer, your first instincts would be to do a trigger, but that is not always the best solution.  There are many considerations when writing a trigger.  Do you know how a trigger works?

Triggers can be useful in many situations but at the same time can cause headaches during large data loads and batch processes.  Many triggers will fire off twice during the process.  Triggers will execute every time a record is modified.   For example, in the order of execution all before and after triggers are executed before workflow rules.  If a workflow rule updates field, the triggers will be executed one more time.  Undelete triggers only work for specific objects.  Record history is not written until after a trigger is completed which makes it impossible to capture the change in a trigger.  Even though there are ways around preventing the trigger from executing during a large data load or batch process, understanding that the trigger how a trigger functions may save a lot of rework when introducing new process into the organization.

Who will support the process when you are not there?

As a programmer, writing a trigger may seem like the best way to go but not all Administrators know Apex code.  One challenge I have been faced with on several projects is the rule of “Do not write code unless you absolutely have to.”  Understanding the business logic is key.  Can you accomplish the function through formula fields or workflow rules?  If you are not going across non related objects, the function maybe resolved through doing a workflow rule involving an update field. Updating fields on related objects can also be done with workflow rules but will depend on the relationship between objects.  Workflow rules can set off a series of procedures in a database.  Once filter criteria is setup for a workflow rule and a record meets those conditions, an action will occur such as an email alert, field update, outbound message, or task.

Formula fields may provide another away to avoid writing a trigger.  Formula fields are automatically calculated when a user views the record and are read-only.  These fields are created the same a custom field is written.  There are limitations on how many characters can be used to write the formula but the formula can span across two related objects.  Even though some formula fields can be complex, the ease of updating how the information is transformed onto the screen maybe easier than writing a trigger for an Administrator.

Workflow rules and formula fields are only two examples of accomplishing a solution that does not involve writing a trigger.  Triggers are useful when there is a developer on the team.  They cannot be updated in the production environment.  Before a trigger is written take the time consider other solutions that can be support by the test coverage and promotion.

 

For more information on Triggers or to contact the CRM manager Development Team Contact Us