# If a Contact is marked as Is_Primary__c = true, automatically uncheck Is_Primary__c on all other sibling Contacts under the same Account.

**Topic:** Development · **Difficulty:** Medium · **Asked at:** Deloitte, Salesforce, Slalom · **Tags:** apex-trigger, trigger, coding, scenario, recursion

Read it on the site: https://www.forcechallenges.com/interview-questions/if-a-contact-is-marked-as-is-primary-c-true-automatically-uncheck-is-primary-c-on-all-other-sibling-contacts

## Answer

Use `after insert, after update`, find sibling primaries on the same Accounts (excluding the triggering records), uncheck them, and guard against recursion.

```apex
trigger ContactTrigger on Contact (after insert, after update) {
    if (ContactHandler.running) return;
    Map<Id, Id> primaryByAcc = new Map<Id, Id>();
    for (Contact c : Trigger.new) if (c.Is_Primary__c && c.AccountId != null) primaryByAcc.put(c.AccountId, c.Id);
    if (primaryByAcc.isEmpty()) return;
    List<Contact> sibs = [SELECT Id, AccountId FROM Contact WHERE AccountId IN :primaryByAcc.keySet()
                          AND Is_Primary__c = true AND Id NOT IN :Trigger.new];
    for (Contact s : sibs) s.Is_Primary__c = false;
    ContactHandler.running = true;
    update sibs;
    ContactHandler.running = false;
}
```
`ContactHandler.running` is a static Boolean used here only to stop the immediate re-fire.

---
Source: ForceChallenges · last updated 2026-10-19
