+ Start a Discussion
Nagaraju Mogili 33Nagaraju Mogili 33 

Whenever an account associated with more than 2 contacts, that shouldn't be deleted, How can we fulfill this one with trigger.

KdKomalKdKomal
Hi Nagaraju,

Please refer the below code for prevention of deletion of accounts which have more than 2 contacts associated with them.

trigger preventDelete on Account (before delete) {
    // Get an aggregate Result per account i.e no of account associate with each Account in Trigger.Old
    List<AggregateResult> groupedResult = [SELECT AccountId,Count(Id)Cont FROM Contact where AccountId IN : Trigger.old group by AccountId ];
    
    // Create a map of accountId and its associated Contacts
    Map<String,Integer> actToContact =  new Map<String,Integer>();
    
    for(AggregateResult ar: groupedResult){
        // Type Cast is done as the returned values are of object Type.
        actToContact.put((String)ar.get('AccountId'), (Integer)ar.get('Cont'));
    }
    
    // Add error for the accounts which have 2 or more contacts 
    for(String actId : actToContact.keySet()){
        if(actToContact.get(actId) > 2){
            Trigger.oldMap.get(actId).addError('Cannot delete Account associated with multiple contacts');
        }
    }
}

Please mark it as the best answer if it solves the issue.
        
Ajay K DubediAjay K Dubedi
Hi Nagaraju,
Here is a trigger by which you can stop deletion of Account which is associated with more than 2 Contacts ---

------------------Trigger Part ---------------------
trigger AccContactAssTrigger on Account (before delete) {

    if(Trigger.isBefore && Trigger.isDelete)
    {
        AccountConAssHandler.stopAccountDeletion(Trigger.Old);
    }
}

-------------Its Handler Class ---------------------------

public class AccountConAssHandler {
    public static void stopAccountDeletion(List<Account> accList)
    {
        Map<Id,Integer> accConMap = new   Map<Id,Integer>();
        List<Contact> conList = new List<Contact>();
        System.debug('accList--' + accList);
        if(accList.size()>0)
        {
            conList = [Select Id,Name, AccountId from Contact where AccountId IN : accList ];
        }
        System.debug('conList ----' + conList);
        if(conList.size()>0)
        {
            for(Contact con : conList)
            {
                if(!accConMap.containsKey(con.AccountId))
                {
                    accConMap.put(con.AccountId, 1);
                }
                else
                {
                    Integer count = 0;
                    count = accConMap.get(con.AccountId);
                    count++;
                    accConMap.put(con.AccountId, count);
                }
            }
        }
        for(Account acc : accList)
        {
            Integer sumConSize = 0;
            sumConSize =  accConMap.get(acc.Id);
            System.debug('sumConSize ---' + sumConSize);
            if(sumConSize>2)
            {
                acc.addError('Cannot Delete Account Which is associated With more than 2 contacts');
            }
        }
    }
}


If you found this answer helpful then please mark it as best answer so it can help others.

Thanks 
Ajay Dubedi