Apex Triggers - 17 (Deloitte Interview Question)
1. Scenario Overview This automation solves a Deloitte Interview Question focused on enforcing data integrity across parent-child relationships . The contact object features a custom checkbox field, Primary_Contact__c . The requirement mandates that each Account can have at most one primary contact . If a user tries to create or update a contact to be a duplicate primary contact under the same account—or re-parents an existing primary contact to an account that already contains one—the transaction must block and throw an error . Because data validation must block execution before commit phases, these criteria execute entirely in before insert and before update context steps . or Enforce a single Primary Contact on an Account. 2. Apex Handler Class java public class ContactTriggerHandler { public static void preventDuplicatePrimaryContacts ( List < Contact > newContacts , Map < Id , Contact > oldContactMap ) { Set < Id > accountIds...