Apex Triggers

Apex Triggers

What is an Apex Trigger?

An Apex Trigger is a block of code that automatically runs before or after a record is created, updated, deleted, or restored in Salesforce. 

|Think of a Trigger as a rule that says: “When this happens, do that.” 

For example: 
If a new Contact is created, a trigger can automatically send a welcome message or assign the contact to a default sales rep. 

When Do We Use Triggers?

Triggers are helpful when:
    • You want to automate business processes.
   • You need to validate or manipulate data when users interact with records.
   • You want to enforce custom rules beyond what standard Salesforce automation can handle (like Validation Rules o r Workflow Rules).

Basic Trigger Syntax

trigger TriggerName on ObjectName (events) { 

    // trigger logic here 

} 

Example:

trigger AccountTrigger on Account (before insert) { 

    for (Account acc : Trigger.new) { 

        acc.Name = acc.Name + ‘ – Verified’; 

    } 

} 

Explanation:

Every time a new Account is created, it adds “- Verified” at the end of the account name. 

Example 1: Set Default Value on Insert

Use Case: When a new Contact is created, set the LeadSource field to “Web” if it’s empty. 

trigger SetDefaultLeadSource on Contact (before insert) { 

    for (Contact c : Trigger.new) { 

        if (String.isBlank(c.LeadSource)) { 

            c.LeadSource = ‘Web’; 

        } 

    } 

} 

Explanation:

This trigger runs before a new Contact is saved.
It checks if the LeadSource field is empty (null or blank).
If it’s blank, it sets the LeadSource to “Web”.
Use Case: Helps assign a default value to keep data consistent when users forget to fill in a field.

Example 2: Prevent Deletion of Important Records

Use Case: Don’t allow deletion of Account records with the type “Customer”. 

trigger PreventCustomerAccountDelete on Account (before delete) { 

    for (Account acc : Trigger.old) { 

        if (acc.Type == ‘Customer’) { 

            acc.addError(‘You cannot delete Customer accounts.’); 

        } 

    } 

Explanation:

• This trigger runs before an Account is deleted.
• It checks if the Type of the Account is “Customer”.
• If yes, it blocks the deletion and shows an error message to the user.
Use Case: Protects important customer records from being accidentally deleted.

Example 3: Auto-Update a Field on Update

Use Case: When a Case is updated, automatically change the Status to “In Progress” if it’s currently “New”. 

trigger AutoUpdateCaseStatus on Case (before update) { 

    for (Case c : Trigger.new) { 

        Case oldCase = Trigger.oldMap.get(c.Id); 

        if (oldCase.Status == ‘New’) { 

            c.Status = ‘In Progress’; 

        } 

    } 

} 

Explanation:

This trigger runs before a Case is updated.
It checks if the previous status was “New”.
If yes, it automatically changes the status to “In Progress”.
Use Case: Automatically move cases forward in the workflow when any change occurs.

Example 4: Add Suffix to Name

Use Case: Add “- Verified” to every new Account name on creation. 

trigger AddSuffixToAccount on Account (before insert) { 

    for (Account acc : Trigger.new) { 

        acc.Name = acc.Name + ‘ – Verified’; 

    } 

Explanation:

• This trigger runs before a new Account is saved.
• It adds ” – Verified” at the end of the Account’s name.
• Use Case: Helps identify new accounts with a standard label for verification or processing.

Example 4: Add Suffix to Name

Use Case: Add “- Verified” to every new Account name on creation. 

trigger AddSuffixToAccount on Account (before insert) { for (Account acc : Trigger.new) { acc.Name = acc.Name + ‘ – Verified’; } }

Explanation:

• This trigger runs before a new Account is saved.
• It adds ” – Verified” at the end of the Account’s name.
Use Case: Helps identify new accounts with a standard label for verification or processing.

Use Case Trigger Type
Auto-assign leads to a user after insert
Prevent deletion of VIP records before delete
Add default value if empty before insert/update
Log record changes after update

Conclusion:

Apex Triggers are a powerful feature that lets developers build automated and intelligent systems within Salesforce. They’re essential for any backend automation or complex validations. 

Tasks:

1-Write a trigger on the Contact object that sets the Lead Source field to "Web" when a new Contact is created and no lead source is provided.

2- Write a trigger on the Account object that prevents deletion if the Account type is "Customer".

3-Write a trigger to automatically set the Rating field to "Hot" for new Leads coming from the industry "Technology".

4-Write a trigger that updates the Description field of an Account to "Updated Record" whenever an Account is edited.

5-Create a trigger that prevents users from creating Opportunities with Amount less than 1000.

6-Write a trigger that sends a debug log message when a new Case is created. .