When records are inserted, updated, deleted, or undeleted from a specific object, an Apex Trigger can run Apex code to perform any automation needed relating to that data. Here are some of the most common use cases:
Defaulting Data
When a new record is created, it is often missing data. An Apex Trigger can see if a field is blank and if so, populate it with a default value. Alternatively, it can look at other data on the record and based on that data, dynamically choose what a missing fieldâs value will be.
Similarly, when a record is updated, you can see if a field is blank and populate it with the necessary default value.
You can also create other records as needed too. For example, it may be the case that when an Opportunity is created, there should always be certain Opportunity Products added automatically. Or perhaps, itâs more dynamic in that based on the Opportunity created, different Opportunity Products are automatically created.
Validation
Another use case for Apex Triggers is validation â however, you should strive to use Validation Rules first, whenever possible, since theyâre easier to define and manage than an Apex Trigger.Â
An Apex Trigger can perform all the validation a Validation Rule can and more, such as:
- Required Validation
- Conditional Validation
- Min & Max Validation
- Etc.
As mentioned before, you should only use an Apex Trigger for validation that isnât possible through a Validation Rule. For example, Validation Rules canât use child records. Sometimes you will need to look at a recordâs child records in order to perform validation on the parent record, i.e. to send an invoice to a customer, the invoice must have at least one invoice line record created to ensure thereâs at least one charge.
Also, Validation Rules do not support delete. If you need to do validation when a record is deleted, an Apex Trigger will have to be used. For example, if you try to delete an order, it can only be deleted if the order is empty, which means it has no order line items in it.
Complex Business Logic (Not Viable Declaratively)
Whenever possible, implement functionality using declarative means, such as Flow, Validation Rules, etc. so itâs easier to maintain and faster to implement.
However, declarative options arenât always possible. For example, if the business logic is too complex, say 50 or more elements in a flow which is arbitrary, then it may be easier to implement with Apex code. Thatâs because the code’s instructional density is much higher. Put differently, you can express the same instructions with much less code than in a flow â and itâs likely to run faster too.
A hybrid approach is possible. A flow can invoke Apex, and Apex code can invoke a flow; itâs not an all or nothing approach.
Data Aggregation
Data aggregation involves computing some value(s) from one or more child records and storing them on a parent record.
A common example is a roll-up summary field where you can display a count or sum on a parent record based on its child records. Roll-up summaries are great for Master-Detail relationships but arenât supported for Lookup relationships.
To recalculate the aggregated data onto the parent record whenever a child record is inserted, updated, and deleted, a Trigger is used to recalculate the data and store it on the parent records. This is a very common scenario which is why there are several AppExchange applications that provide this functionality.
Another example is that accounts sometimes have memberships or subscriptions as child records but they only have one âCurrent Membershipâ or âCurrent Subscriptionâ lookup on the account to that corresponding child record. A Trigger on the underlying child object can maintain that âCurrent Membershipâ or âCurrent Subscriptionâ lookup on the account whenever a membership or subscription record is inserted, updated, or deleted. Using this approach, you can then use account formulas to read the key membership or subscription data elements for the current information.
In the next lesson, we cover the basics of implementing a Trigger, along with some examples.