function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sagar077sagar077 

Create an apex trigger that will count number of contacts associated with an account(create a field at account level) . Must update the count in insertion and deletion of a contact by using Map..help me plz

Hi

Create an Apex trigger that will count the number of contacts associated with an account(create a field at account level). Must update the count in insertion and deletion of contact by using Map.

I am done with using map but i am unable to complete one case ie- if account having associated with 2 contacts and I changed the Account Name 'A' to Account 'B' then the count of contact fields not update the count of contact.

Pleases help me in code
Thanks 

trigger AccountCountContactMap on Contact (after insert,after update, after delete)
{
        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> listAcc = 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.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)
        {
            listAcc =[Select Id,Number_Of_Contact_Count__c from Account where Id in : AcctIds];
            for(Account acct : listAcc)
                {
                    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_Contact_Count__c = acct.Number_Of_Contact_Count__c == null ? noOfConts : (acct.Number_Of_Contact_Count__c + noOfConts);
                }
            update listAcc;
        }
}

plz help me
ANUTEJANUTEJ (Salesforce Developers) 
Hi Sagar,

I think you should be able to use flow to count the number of contacts per account and update the field as per the number, below are the screenshots that could help:

1]Flow Overview
User-added image
2]Get Record elementUser-added image
User-added image
User-added image
3]LoopUser-added image

I will upload the next two elements in another comment as the max limit is reached and could you please try this way.

Regards,
Anutej
ANUTEJANUTEJ (Salesforce Developers) 
4]Assignment elementUser-added image5]update records
User-added image