Master Apex Trigger Scenarios with Salesforce

 

Master Apex Trigger Scenarios with Salesforce

Introduction

Apex Triggers are essential for automating business processes and ensuring data consistency within Salesforce. This guide covers real-world Apex Trigger scenarios to help developers streamline their workflows and enhance Salesforce efficiency.


1. Update Latest Case Number on Account

Scenario:

When a new Case is created, update the Latest Case Number field on the associated Account.

Trigger Example:

trigger UpdateLatestCaseNumber on Case (after insert) {
    Set<Id> accountIds = new Set<Id>();
    for (Case c : Trigger.new) {
        if (c.AccountId != null) {
            accountIds.add(c.AccountId);
        }
    }
    
    List<Account> accountsToUpdate = [SELECT Id FROM Account WHERE Id IN :accountIds];
    for (Account acc : accountsToUpdate) {
        acc.Latest_Case_Number__c = [SELECT CaseNumber FROM Case WHERE AccountId = :acc.Id ORDER BY CreatedDate DESC LIMIT 1].CaseNumber;
    }
    update accountsToUpdate;
}

2. Populate Recent Opportunity Amount on Account

Scenario:

When a new Opportunity is created, update the Recent Opportunity Amount field on the related Account.

Trigger Example:

trigger UpdateRecentOpportunityAmount on Opportunity (after insert) {
    Set<Id> accountIds = new Set<Id>();
    for (Opportunity opp : Trigger.new) {
        if (opp.AccountId != null) {
            accountIds.add(opp.AccountId);
        }
    }
    
    List<Account> accountsToUpdate = [SELECT Id FROM Account WHERE Id IN :accountIds];
    for (Account acc : accountsToUpdate) {
        acc.Recent_Opportunity_Amount__c = [SELECT Amount FROM Opportunity WHERE AccountId = :acc.Id ORDER BY CloseDate DESC LIMIT 1].Amount;
    }
    update accountsToUpdate;
}

3. Create Related Records Based on Checkbox Fields

Scenario:

When an Account is created with specific checkboxes selected, automatically create related Contact or Opportunity records.

Trigger Example:

trigger CreateRelatedRecords on Account (after insert) {
    List<Contact> contactsToCreate = new List<Contact>();
    List<Opportunity> opportunitiesToCreate = new List<Opportunity>();
    
    for (Account acc : Trigger.new) {
        if (acc.Create_Contact__c) {
            contactsToCreate.add(new Contact(FirstName = 'Auto', LastName = 'Created', AccountId = acc.Id));
        }
        if (acc.Create_Opportunity__c) {
            opportunitiesToCreate.add(new Opportunity(Name = 'Auto Opportunity', AccountId = acc.Id, CloseDate = Date.today().addDays(30), StageName = 'Prospecting'));
        }
    }
    insert contactsToCreate;
    insert opportunitiesToCreate;
}

4. Populate Rating as 'Hot' for Media Industry Accounts

Scenario:

When a new Account is created in the Media Industry, set the Rating field to 'Hot'.

Trigger Example:

trigger SetHotRatingForMediaAccounts on Account (before insert) {
    for (Account acc : Trigger.new) {
        if (acc.Industry == 'Media') {
            acc.Rating = 'Hot';
        }
    }
}

5. Populate Description for High-Value Opportunities

Scenario:

When an Opportunity is created with an Amount greater than $100,000, populate the Description field with 'Hot Opportunity'.

Trigger Example:

trigger HighlightHighValueOpportunities on Opportunity (before insert) {
    for (Opportunity opp : Trigger.new) {
        if (opp.Amount != null && opp.Amount > 100000) {
            opp.Description = 'Hot Opportunity';
        }
    }
}

6. Copy Billing Address to Shipping Address

Scenario:

If the CopyBillingToShipping checkbox is checked during Account creation, copy the Billing Address to Shipping Address.

Trigger Example:

trigger CopyBillingToShipping on Account (before insert, before update) {
    for (Account acc : Trigger.new) {
        if (acc.CopyBillingToShipping__c) {
            acc.ShippingStreet = acc.BillingStreet;
            acc.ShippingCity = acc.BillingCity;
            acc.ShippingState = acc.BillingState;
            acc.ShippingPostalCode = acc.BillingPostalCode;
            acc.ShippingCountry = acc.BillingCountry;
        }
    }
}

7. Populate Default Values for New Positions

Scenario:

When a new Position record is created, set default values for Open Date, Min Pay, and Max Pay.

Trigger Example:

trigger SetDefaultValuesForPositions on Position__c (before insert) {
    for (Position__c pos : Trigger.new) {
        if (pos.Open_Date__c == null) {
            pos.Open_Date__c = Date.today();
        }
        if (pos.Min_Pay__c == null) {
            pos.Min_Pay__c = 50000;
        }
        if (pos.Max_Pay__c == null) {
            pos.Max_Pay__c = 100000;
        }
    }
}

8. Create Related Contact on Account Creation

Scenario:

When a new Account is created, automatically create a related Contact.

Trigger Example:

trigger CreateContactOnAccountCreation on Account (after insert) {
    List<Contact> contactsToCreate = new List<Contact>();
    for (Account acc : Trigger.new) {
        contactsToCreate.add(new Contact(FirstName = 'Default', LastName = 'Contact', AccountId = acc.Id));
    }
    insert contactsToCreate;
}

9. Create Related Opportunity on Account Creation

Scenario:

When a new Account is created, automatically create a related Opportunity.

Trigger Example:

trigger CreateOpportunityOnAccountCreation on Account (after insert) {
    List<Opportunity> opportunitiesToCreate = new List<Opportunity>();
    for (Account acc : Trigger.new) {
        opportunitiesToCreate.add(new Opportunity(Name = 'New Business', AccountId = acc.Id, CloseDate = Date.today().addDays(30), StageName = 'Prospecting'));
    }
    insert opportunitiesToCreate;
}

Conclusion

By mastering these Apex Trigger Scenarios, you can automate Salesforce processes, improve efficiency, and ensure data accuracy. Implement these triggers to elevate your Salesforce expertise and streamline workflows!

Comments

Popular posts from this blog

πŸš€ Hiring Alert | Remote Salesforce Developer Opportunity! πŸš€