By: Ankita Rustagi

Apex trigger is a script that defines functionality and will execute before or after DML operations on a record. DML operations such as: insert, update, delete, undelete of a record.

For example, when we want to update a field on a child record upon save of a change on the master record we will write an after update trigger on the master object. 

Apex triggers can be executed after a number of different actions have been taken within the Salesforce system:

  • before insert
  • before update
  • before delete
  • after insert
  • after update
  • after delete
  • after undelete

There are context variables in trigger, defined below:

Trigger syntax is defined below:

Trigger example: trigger on Contact which will create an Account record whenever a Contact is created without an Account.

Why do we use triggers?

Many things in Salesforce can be configured using declarative programming, i.e. click or drag and drop solution. Some requirements need customizable programming, which includes triggers. Triggers are event based, and these events are DML operations as described above.

Triggers are Powerful: They are simply more powerful than non-code automation options. Workflow Rules and Processes can only run in two situations, after a record is created or after it is updated. Triggers can fire before or after creating, updating, or deleting records. They can also fire after undeleting records.

Triggers are faster: Flows in Salesforce are capable of performing actions nearly as complex as those that can be done by Triggers. Flows will execute more slowly and use more CPU time than Apex Triggers performing the same task.

Writing a test class for trigger is easier:  Writing a test class for trigger is more efficient than having to test complex flows. 

When To Use Triggers

This is not to say you should stop using Flows entirely. As I said in the beginning, Workflow Rules, Process Builder, and Flows are great. Utilize them to your advantage. Although, you will run into situations when these options fall short. If you’ve investigated no-code options and they aren’t as customizable as you need them to be, check out Triggers. If you’re working on a Flow that feels like it’s getting out of hand, consider using a Trigger instead.

Trigger best practices:

1) One Trigger Per Object: A single Apex trigger is all you need for one particular object. Multiple triggers for a single object can make controlling the order of execution difficult.

2) Bulkify your Code: It means code is capable of handling more than one record.

3) Avoid SOQL Queries or DML statements inside FOR Loops: An individual Apex request gets a maximum of 100 SOQL queries. So if a trigger is invoked by a batch of more than 100 records, governor limits will throw a runtime exception.

4) Using Collections: Use Apex collections like map, list, set to store data. This makes the trigger more efficient.

5) Avoid complex logic in triggers: To simplify testing and reuse, triggers should delegate to Apex classes which contain the actual execution logic.