Posts

Showing posts from June, 2026

Trigger 4 - Trigger to prevent duplication of an Account record based on Name whenever a record is inserted or updated.

 Trigger 4 scenario - Trigger to prevent duplication of an Account record based on Name whenever a record is inserted or updated. trigger AccountTrigger on Account (before insert , before update ) { if (Trigger.isBefore) { if (Trigger.isInsert) { AccountTriggerHandler.preventDuplicateNames(Trigger. new , null ); } else if (Trigger.isUpdate) { AccountTriggerHandler.preventDuplicateNames(Trigger. new , Trigger.oldMap); } } } public class AccountTriggerHandler {     public static void preventDuplicateNames(List<Account> newAccounts, Map<Id, Account> oldAccountMap) {         Set<String> newAccountNames = new Set<String>();         List<Account> recordsToValidate = new List<Account>();         // 1. Isolate the names being added or changed         for (Account acc : newAccounts) {   ...

Trigger 3 - Roll up Summary Trigger

 Trigger to count the number of Contacts associated with an Account and display the Contacts count on the Account's custom field. trigger ContactTrigger on Contact (after insert, after update, after delete, after undelete) {     if (Trigger.isAfter) {         if (Trigger.isInsert || Trigger.isUndelete) {             ContactTriggerHandler.countContacts(Trigger.new, null);         }          else if (Trigger.isUpdate) {             ContactTriggerHandler.countContacts(Trigger.new, Trigger.oldMap);         }          else if (Trigger.isDelete) {             ContactTriggerHandler.countContacts(null, Trigger.oldMap);         }     } } public class ContactTriggerHandler {          public static void countContacts(List<Contact...

Trigger 2 - Whenever a Contact's description is updated, then its Parent Account's description should also get updated by it.

 Whenever a Contact's description is updated, then its Parent Account's description should also get updated by it. trigger AccountTrigger on Account (after update) {     if (Trigger.isAfter && Trigger.isUpdate) {         AccountTriggerHandler.handleAfterUpdate(Trigger.new, Trigger.oldMap);     } } public with sharing class AccountTriggerHandler {          public static void handleAfterUpdate(List<Account> newAccounts, Map<Id, Account> oldAccountMap) {         Set<Id> accountIdsWithUpdatedPhone = new Set<Id>();                  // Loop 1: Find parent accounts where the target field actually changed         for (Account newAcc : newAccounts) {             Account oldAcc = oldAccountMap.get(newAcc.Id);                     ...

Trigger 1 - Whenever Account's Phone field is updated, all related Contacts' Phone field should also be updated with the parent Account's Phone.

 Whenever Account's Phone field is updated, all related Contacts' Phone field should also be updated with the parent Account's Phone. trigger AccountTrigger on Account (after update) {     if (Trigger.isAfter && Trigger.isUpdate) {         AccountTriggerHandler.handleAfterUpdate(Trigger.new, Trigger.oldMap);     } } public with sharing class AccountTriggerHandler {          public static void handleAfterUpdate(List<Account> newAccounts, Map<Id, Account> oldAccountMap) {         Set<Id> accountIdsWithUpdatedPhone = new Set<Id>();                  // Loop 1: Identify accounts where the Phone field actually changed         for (Account newAcc : newAccounts) {             Account oldAcc = oldAccountMap.get(newAcc.Id);                ...