Apex Trigger Examples

  • AccountTrigger.apxt
trigger AccountTrigger on Account (after insert) {
    AccountTriggerHandler handler = new AccountTriggerHandler();
    
    switch on trigger.operationType {
        when AFTER_INSERT {
            handler.afterInsert(Trigger.Trigger.newMap);
        }
    }
}
  • AccountTriggerHandler.apxc
public class AccountTriggerHandler {
    public void afterInsert(List<Account> accounts) {
        List<Contact> contacts = new List<Contact>();
        
        for (Account account : accounts) {
            Contact c = new Contact(LastName=account.Name,
                                    AccountId=account.Id);
            contacts.add(c);
        }
        
        insert contacts;
    }
}
  • AccountTrigger_Test.apxc
@isTest
public class AccountTrigger_Test {
    @isTest
    static void TestBulkInsert() {
        List<Account> accounts = new List<Account>();
        
        for (Integer i = 0; i < 300; i++) {
            Account a = new Account(Name='Test');
            accounts.add(a);
        }
        
        Test.startTest();
        insert accounts;
        Test.stopTest();
    }
    
    @isTest
    static void TestContactLinkingOnInsert() {
        Account a = new Account(Name='Test');
        
        insert a;
        
        List<Contact> c = [SELECT Id, AccountId
                             FROM Contact
                            WHERE (AccountId = :a.Id)];
        
        System.assert(c.size() >= 1);
    }
}
  • AccountTrigger.apxt
trigger AccountTrigger on Account (after update) {
	AccountTriggerHandler handler = new AccountTriggerHandler();
    
    switch on trigger.operationType {
        when AFTER_UPDATE {
            handler.afterUpdate(Trigger.oldMap, Trigger.newMap);
        }
    }
}
  • AccountTriggerHandler.apxc
public class AccountTriggerHandler {
    
    public void afterUpdate(Map<Id,Account> oldMap, Map<Id,Account> newMap) {
        List<Id> accountIds = new List<Id>();
        
        // Check whether each Owner is updated in each updated Account
        for (Account account : newMap.values()) {
            if (account.OwnerId != oldMap.get(account.Id).OwnerId) {
                accountIds.add(account.Id);
            }
        }
        
        // Check if 1+ Account was updated with new Owner
        if (accountIds.size() != 0) {
            List<Contact> contacts = [SELECT Id, OwnerId, AccountId
                                        FROM Contact
                                       WHERE AccountId IN :accountIds];
            
            // Update Contact Owner ID with Owner Id from Account
            for (Contact c : contacts) {
                c.OwnerId = newMap.get(c.AccountId).OwnerId;
            }
            update contacts;
        }
    }
}
  • AccountTrigger_Test.apxc
// TBD

3. Create a new Opportunity whenever an Account is created or updated for Industry of Agriculture:

// TBD

4. Users should not be able to delete any Account with an associated Contact:

// TBD

5. Only Admin can delete Closed Opportunities and an email should be sent to Owners for Closed Won Opportunities:

// TBD

6. Write the count of total Contacts to their associated Account:

// TBD
// TBD

8. Whenever a new active User having profile System Administrator is inserted, add the user to the public group “Admins”:

// TBD

9. Update example 8 such that whenever a User's profile is changed from or to System Administrator and the User is marked active or inactive, add or delete the user to the public group “Admins”:

// TBD