How to write APEX Triggers and What are the best practices?

Apex triggers allows you to perform some actions when any record is created, updated, deleted or undeleted in Salesforce.

What can we do using triggers?

A trigger can perform following actions:

  • Insert, Update, Delete records.
  • Update fields from child to parent and vice-versa.
  • Send Emails to record owner, external user, contact & lead etc.
  • Call @future methods to execute logic asynchronously.
  • Invoke web service callouts, REST and SOAP call etc.

Trigger Syntax:

trigger TriggerName on ObjectName (trigger_events) {

When are Triggers executed?

Triggers are only executed whenever any DML (Insert, Update, Delete) or Undelete happens on sObject in Salesforce. DML can happen from any source like DataLoader, Salesforce UI or any other tools, and triggers will run immediately.

We cannot instantiate triggers and also don’t have any control over the execution of triggers unlike apex classes. We can only activate/deactivate triggers.

Types of Triggers:

There are two types of apex triggers “Before” and “After”. Here is the difference between both:

Before Trigger After Trigger
The record is Read/Write. The record is Read-Only
We can access/validate/update the record. We can only access the record.
Record is not saved to database. Record is saved to database.
Record Id is not available (Only if record is new ) Record Id is always available

Trigger Events:

Trigger events play a role to execute the triggers. We define events in the definition of trigger body.

There are several types of events:-

  • Before Insert – Record is inserted but not saved to database.
  • After Insert – Record is inserted and saved to database.
  • Before Update – Record is updated but not saved to database.
  • After Update – Record is updated and saved to database.
  • Before Delete –Record is deleted and available in Recycle bin.
  • After Undelete –Record is retrieved from Recycle bin and inserted in Salesforce.

Trigger Context Variables:

There are several context variables defines the prefix “Trigger” which comes up with:

  • New – contains new values when record is inserted or updated.(Available only in Insert and Update events).
  • newMap – contains map of new values when record is inserted or updated.
  • Old – contains old values when record is updated or deleted. (Available only in Update and Delete events).
  • oldMap – contains map of old values when record is updated or deleted.
  • isNew – returns true if record is inserted.
  • isBefore – returns true if trigger_event is before.
  • isAfter –returns true if trigger_event is after.
  • isInsert – returns true if trigger_event is before insert or after insert.
  • isUpdate –returns true if trigger_event is before update or after update.
  • isDelete –returns true if trigger_event is before delete or after delete.

For e.g. We want to update the Fax field of Account when account is inserted. We need to use “Before Insert” trigger event as record can be set only in before triggers.

Trigger UpdateAccName on Account (Before Insert) {
//Getting new values
For (Account a: Trigger. New) {
a.Fax = ‘011566779’;

Key Considerations while writing triggers:

As a best practice, always write one trigger per object. If we write more than one trigger on an object, we don’t have control on the order in which triggers are executed.  Also use a trigger handler (APEX Class) to write the logic of trigger which helps us to keep the logic in place and avoid multiple triggers.

For example if you want to implement the above code using trigger handler, create an APEX class and make a call inside the trigger:

Trigger UpdateAccName on Account (Before Insert) {
if (Trigger.isBefore && Trigger.isInsert){
Public class AccountUtil{
  public static void updateAccount(List<Account> newAccList){
   For (Account a: newAccList) {
    a.Fax = ‘011566779’;

Please check the following additional resources

  • Trigger Context Variables:

  • Best Practices while writing triggers:

  • Getting Started With Apex Triggers:


Senior Salesforce Architect
In Connect me on LinkedIn
twitter Follow me on Twitter @apexcoder
blogger Subscribe to my blog
email Email me

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s