+ Start a Discussion
srikanth j 24srikanth j 24 

Trigger to count contact value on account object

Hi Guys
When a contact created i want to count of that contacts value on Account object Using Trigger 
Can anyone give me a solution for this 
Advance Thanks
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same
1) http://www.infallibletechie.com/2013/09/trigger-to-count-number-of-contacts.html
2) http://www.dhruvsoft.com/blog/displaying-total-number-of-contacts-for-each-account-in-salesforce/
 
trigger ContactCount on Contact (after insert, after update, after delete, after undelete) {
    Map<Id, List<Contact>> mapAcctIdContactList = new Map<Id, List<Contact>>();
    Map<Id, List<Contact>> mapAcctIdDelContactList = new Map<Id, List<Contact>>();
    Set<Id> AcctIds = new Set<Id>();    
    List<Account> listAcct = new List<Account>();
    
    if(trigger.isInsert) {
        for(Contact Con : trigger.New) {
            if(String.isNotBlank(Con.AccountId)) {
                if(!mapAcctIdContactList.containsKey(Con.AccountId)) {
                    mapAcctIdContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdContactList.get(Con.AccountId).add(Con); 
                AcctIds.add(Con.AccountId);
            }   
        }  
    }
    
    if(trigger.isUpdate) {
        for(Contact Con : trigger.New) {
            if(String.isNotBlank(Con.AccountId) && Con.AccountId != trigger.oldMap.get(Con.Id).AccountId) {
                if(!mapAcctIdContactList.containsKey(Con.AccountId)){
                    mapAcctIdContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdContactList.get(Con.AccountId).add(Con); 
                AcctIds.add(Con.AccountId);
            } else if(String.isBlank(Con.AccountId) && String.isNotBlank(trigger.oldMap.get(Con.Id).AccountId)) {
                if(!mapAcctIdDelContactList.containsKey(Con.AccountId)){
                    mapAcctIdDelContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdDelContactList.get(Con.AccountId).add(Con);   
                AcctIds.add(trigger.oldMap.get(Con.Id).AccountId);
            }
        }  
    }
    
    if(trigger.isUndelete) {
        for(Contact Con : trigger.new) {
            if(String.isNotBlank(Con.AccountId)){
                if(!mapAcctIdContactList.containsKey(Con.AccountId)){
                    mapAcctIdContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdContactList.get(Con.AccountId).add(Con);     
                AcctIds.add(Con.AccountId);
            }
        }  
    }      

    if(trigger.isDelete) {
        for(Contact Con : trigger.Old) {
            if(String.isNotBlank(Con.AccountId)){
                if(!mapAcctIdDelContactList.containsKey(Con.AccountId)){
                    mapAcctIdDelContactList.put(Con.AccountId, new List<Contact>());
                }
                mapAcctIdDelContactList.get(Con.AccountId).add(Con);    
                AcctIds.add(Con.AccountId); 
            }
        }  
    }   
    
    if(AcctIds.size() > 0) {
        listAcct = [SELECT Id, Number_of_Contacts__c FROM Account WHERE Id IN : AcctIds];
        
        for(Account acct : listAcct) {
            Integer noOfConts = 0;
            if(mapAcctIdContactList.containsKey(acct.Id)) {
                noOfConts += mapAcctIdContactList.get(acct.Id).size();
            }
            if(mapAcctIdDelContactList.containsKey(acct.Id)) {
                noOfConts -= mapAcctIdDelContactList.get(acct.Id).size();
            }
            acct.Number_of_Contacts__c = acct.Number_of_Contacts__c == null ? noOfConts : (acct.Number_of_Contacts__c + noOfConts);
        }
        
        update listAcct;    
    }
}

Let us know if this will help you
 
Harish RamachandruniHarish Ramachandruni
Hi ,


Use this code 

 
trigger countTotalContactInAccount on Contact (after insert,after update,after delete) {
    set<id> accId =new set<id>();
    List<Account> accountToUpdate = new List<Account>();
    if(trigger.isInsert){
        for(Contact c:trigger.new){
            accId.add(c.accountID);
        }
    }
    if(trigger.isUpdate|| trigger.isDelete){
        for(Contact c:trigger.old){
            accId.add(c.accountID);
        }
    }
    Map<Id,Account> accountMap =new Map<Id,Account>([select id,name,Number_of_Contact__c from  Account where Id IN: accId ]);
    
       for(Account acc:[select id,name,Number_of_Contact__c,(Select id from Contacts) from Account where Id IN: accId]){
           accountMap.get(acc.id).Number_of_Contact__c = acc.Contacts.size();
           accountToUpdate.add(accountMap.get(acc.id));
       }
       update accountToUpdate;
}




Regards,
Harish.R.
Dilip_VDilip_V
Hello Srikanth,


1.Create a field called 'No of contacts' on account.
2.Use this trigger.
trigger CountContactsnew on Contact (after insert, after delete, after undelete) {

    List<id> accIdList = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete){
        For(Contact con1 : Trigger.new){
            accIdList.add(con1.accountid);
        }
    }
    if(Trigger.isDelete){
        For(Contact con1 : Trigger.old){
            accIdList.add(con1.accountid);
        }
    }
    List<Account> accUpdateList = new List<Account>();
    For(Account acc : [SELECT No_Of_Contacts__C,(SELECT id FROM Contacts) FROM Account WHERE id =: accIdList]){
        acc.No_Of_Contacts__C = acc.Contacts.size();
        accUpdateList.add(acc);
    }
    try{
        update accUpdateList;
    }Catch(Exception e){
        System.debug('Exception :'+e.getMessage());
    }
}

If it helps make it as best answer.

Thanks,
Dilip.