By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
horizontal-light horizontal-dark
  • Home
  • Tutorials
    • Salesforce AI
    • Salesforce DevOps
    • Career
    • Errors
    • Interview Questions
    • Salesforce Integration
    • Salesforce Flow
  • Salesforce Tools
  • Apex Development
  • Lightning Web Components
  • Salesforce Admin
  • About
    • Privacy Policy
    • Disclaimer
    • Terms & Conditions
    • Contact
SalesforceCornerSalesforceCorner
Search
  • Home
  • Tutorials
    • Salesforce AI
    • Salesforce DevOps
    • Career
    • Errors
    • Interview Questions
    • Salesforce Integration
    • Salesforce Flow
  • Salesforce Tools
  • Apex Development
  • Lightning Web Components
  • Salesforce Admin
  • About
    • Privacy Policy
    • Disclaimer
    • Terms & Conditions
    • Contact
Follow US
Salesforce Corner » Apex Development » Apex Trigger in Salesforce: Complete Beginner Guide with Examples
Apex Development

Apex Trigger in Salesforce: Complete Beginner Guide with Examples

Learn Salesforce. Build Skills. Grow Your Career.

Neha Panwar
By
Neha Panwar
ByNeha Panwar
Salesforce Developer and Technical Writer
Neha Panwar is a Salesforce developer and technical writer who creates practical learning resources for Salesforce administrators and developers. She specializes in Salesforce Administration, Apex, Lightning...
Follow:
- Salesforce Developer and Technical Writer
Last updated: 2026/06/08
Share
Salesforce Apex Trigger
SHARE

If you are learning Salesforce development, Apex Triggers are one of the first things you need to master.

Contents
What is an Apex Trigger?When to Use Apex TriggerTrigger Syntax (Basic Structure)Types of TriggersTrigger EventsTrigger Context VariablesReal World Example — Complete TriggerApex Trigger Best PracticesCommon Mistakes Beginners MakeApex Trigger vs Flow — When to Use WhatConclusion

Many beginners feel confused when they first see trigger code. It looks complex, but once you understand the basics, it becomes very simple.

In this guide, you will learn what Apex Triggers are, how they work, and how to write your first trigger with real examples.

What is an Apex Trigger?

An Apex Trigger is a piece of code that runs automatically before or after a record is inserted, updated, deleted, or undeleted in Salesforce.

Think of it like this:

Every time something happens to a record — a trigger can react to it automatically.

Simple example: When a new Account is created, automatically create a related Task for the owner.

When to Use Apex Trigger

Use a trigger when:

  • Flow or Process Builder cannot handle the logic
  • You need complex conditions or calculations
  • You need to update related records automatically
  • You need to validate data before saving

Trigger Syntax (Basic Structure)

java

trigger TriggerName on ObjectName (trigger events) {
    // your code here
}

Real example:

java

trigger AccountTrigger on Account (before insert, after insert) {
    // your code here
}

Types of Triggers

There are two main types:

1. Before Trigger

Runs before the record is saved to the database

Use it when:

  • You want to validate or modify field values before saving
  • Example: Check if phone number is empty before inserting

java

trigger AccountTrigger on Account (before insert) {
    for (Account acc : Trigger.new) {
        if (acc.Phone == null) {
            acc.Phone = '0000000000';
        }
    }
}

2. After Trigger

Runs after the record is saved to the database

Use it when:

  • You want to create or update related records
  • Example: Create a Task after a new Lead is inserted

java

trigger LeadTrigger on Lead (after insert) {
    List<Task> tasks = new List<Task>();
    for (Lead ld : Trigger.new) {
        Task t = new Task();
        t.Subject = 'Follow up with Lead';
        t.WhoId = ld.Id;
        tasks.add(t);
    }
    insert tasks;
}

Trigger Events

You can combine multiple events in one trigger:

EventWhen it runs
before insertBefore new record is saved
after insertAfter new record is saved
before updateBefore existing record is updated
after updateAfter existing record is updated
before deleteBefore record is deleted
after deleteAfter record is deleted
after undeleteAfter record is restored

Trigger Context Variables

These are special variables available inside every trigger:

VariableWhat it contains
Trigger.newList of new records
Trigger.oldList of old records (before update)
Trigger.newMapMap of new records with Id
Trigger.oldMapMap of old records with Id
Trigger.isInsertTrue if insert operation
Trigger.isUpdateTrue if update operation
Trigger.isDeleteTrue if delete operation

Real World Example — Complete Trigger

Scenario: When an Opportunity is marked as Closed Won, automatically create a follow-up Task

java

trigger OpportunityTrigger on Opportunity (after update) {
    List<Task> tasks = new List<Task>();
    
    for (Opportunity opp : Trigger.new) {
        Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
        
        if (opp.StageName == 'Closed Won' && 
            oldOpp.StageName != 'Closed Won') {
            
            Task t = new Task();
            t.Subject = 'Send thank you email to customer';
            t.WhatId = opp.Id;
            t.ActivityDate = Date.today().addDays(1);
            tasks.add(t);
        }
    }
    
    if (!tasks.isEmpty()) {
        insert tasks;
    }
}

Apex Trigger Best Practices

1. One Trigger Per Object Never create multiple triggers on the same object — it causes unpredictable behavior

2. No Logic in Trigger Keep trigger code clean — move all logic to a Handler class

java

trigger AccountTrigger on Account (before insert, after insert) {
    AccountTriggerHandler.handleBeforeInsert(Trigger.new);
}

3. Bulkify Your Code Always write code that can handle multiple records at once — never put SOQL inside a loop

❌ Wrong:

java

for (Account acc : Trigger.new) {
    List<Contact> cons = [SELECT Id FROM Contact WHERE AccountId = :acc.Id];
}

✅ Right:

java

Set<Id> accountIds = new Set<Id>();
for (Account acc : Trigger.new) {
    accountIds.add(acc.Id);
}
List<Contact> cons = [SELECT Id FROM Contact WHERE AccountId IN :accountIds];

4. Always Check for Empty Lists Before DML operations, always check if list is empty

java

if (!tasks.isEmpty()) {
    insert tasks;
}

5. Use Try-Catch for Error Handling Always handle exceptions gracefully

Common Mistakes Beginners Make

1. SOQL inside for loop This hits governor limits very quickly — always move SOQL outside loops

2. Hardcoding values Never hardcode Ids or values — use Custom Labels or Custom Settings

3. Not bulkifying code Always assume multiple records will be processed at once

4. Multiple triggers on same object Always use one trigger per object with a handler class

Apex Trigger vs Flow — When to Use What

ScenarioUse
Simple record updateFlow ✅
Complex calculationsApex Trigger ✅
No code preferredFlow ✅
Related record creation with conditionsApex Trigger ✅
Beginner friendlyFlow ✅

Conclusion

Apex Triggers are a powerful tool in Salesforce development. Once you understand before vs after triggers and context variables, you can automate almost anything.

Start with simple before insert triggers, then move to after insert and after update. Practice on a free Developer org and build real examples.

The more you practice, the more confident you will become.

Found this helpful? Share it with your Salesforce learning community. Have questions? Drop them in the comments below.

Read More:

  • Salesforce Flow Tutorial for Beginners: Complete Step-by-Step Guide (2026)
  • types of salesforce integrations
  • salesforce developer interview questions

TAGGED:apex programmingapex triggerapex trigger best practicesapex trigger examplebefore trigger after triggerbulkify apexsalesforce apexsalesforce developertrigger handler
Share This Article
Facebook Email Print
ByNeha Panwar
Salesforce Developer and Technical Writer
Follow:
Neha Panwar is a Salesforce developer and technical writer who creates practical learning resources for Salesforce administrators and developers. She specializes in Salesforce Administration, Apex, Lightning Web Components (LWC), Flow, integrations, and automation. Through Salesforce Corner, she publishes step-by-step tutorials, coding guides, and real-world solutions designed to help readers understand Salesforce concepts and apply them in projects with confidence.
Leave a Comment Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Latest Post

Salesforce approval process diagram
Salesforce Approval Process: A Practical Guide with Real Business Examples
Salesforce Admin
Salesforce relationship comparison infographic
Master-Detail or Lookup? Choosing the Right Relationship in Salesforce
Salesforce Admin
Salesforce dynamic forms interface illustration
Salesforce Dynamic Forms: A Better Way to Show and Hide Fields
Salesforce Flow
Salesforce flow debugging guide
How to Debug and Fix Salesforce Flow Errors ?
Salesforce Flow
Salesforce Flow Loops tutorial diagram
Salesforce Flow Loops: Collections, Iteration, and Best Practices
Salesforce Flow

Stay Updated with Salesforce Tutorials

Get the latest Salesforce guides, tutorials, and developer tips delivered to your inbox.
slaesforce corner mascot

Explore More Topics

  • salesforce admin
  • salesforce developer
  • Salesforce Admin
  • salesforce tutorial
  • Salesforce Development
  • salesforce automation
  • salesforce apex
  • salesforce security
  • Apex Development
  • lightning web components
  • Lightning Web Components
  • Salesforce Tutorials
  • salesforce lwc
  • Salesforce Tools
  • Salesforce Beginner Guide
horizontal-dark-transparent

Learn Salesforce development with practical tutorials, Apex guides, integration examples, and real-world solutions for developers.

  • Quick Links:
  • About
  • Contact
  • Privacy Policy
  • Disclaimer
  • Terms & Conditions
Facebook Twitter Youtube Linkedin-in

Salesforce Corner © 2026

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?