Trigger should be written on contact object. The below code is working for all listed operations
- Inserting Contact with an Account
- Updating/Switching Accounts on Contacts ( this will update the count on both the Accounts )
- Removing Account from Contact
- Deleting Contact
- Undeleting Contact
trigger ContactTrigger on Contact (after insert,after update, after delete, after undelete) {
if(Trigger.isAfter)
{
Map<Id,List<Contact>> MapOfActIdandConList = new Map<Id,List<Contact>>();
List<Account> lstAct = new List<Account>();
Set<Id> AccountIdSetNew = new Set<Id>();
Set<Id> AccountIdSetOld = new Set<Id>();
if(Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate)
{
for(Contact contNew : Trigger.New)
{
if(contNew.AccountId != null)
{
AccountIdSetNew.add(contNew.AccountId);
}
}
if(AccountIdSetNew.size()>0){
List<Contact> lstContNew = [Select Id,AccountId from Contact where AccountId in: AccountIdSetNew ];
for(Contact ct : lstContNew)
{
if(MapOfActIdandConList.containsKey(ct.AccountId))
{
MapOfActIdandConList.get(ct.AccountId).add(ct);
}
else
{
MapOfActIdandConList.put(ct.AccountId, new List<Contact> {ct});
}
}
}
}
if(Trigger.isDelete || Trigger.IsUpdate)
{
for(Contact contOld : Trigger.Old)
{
if(contOld.AccountId != null)
{
AccountIdSetOld.add(contOld.AccountId);
}
}
if(AccountIdSetOld.size()>0){
List<Contact> lstContOld = [Select Id,AccountId from Contact where AccountId in: AccountIdSetOld ];
for(Contact ct : lstContOld)
{
if(MapOfActIdandConList.containsKey(ct.AccountId))
{
MapOfActIdandConList.get(ct.AccountId).add(ct);
}
else
{
MapOfActIdandConList.put(ct.AccountId, new List<Contact> {ct});
}
}
}
}
for( Id actId : MapOfActIdandConList.keySet())
{
Account act = new Account();
act.Id = actId;
act.Count_Contact__c = MapOfActIdandConList.get(actId).size();
lstAct.add(act);
}
if(lstAct.size()>0)
{
update lstAct;
}
}
}
Congrats on your new site, get it listed here for free and we’ll start sending people to your site https://1mdr.short.gy/submityoursite
Good job on the new site! Now go ahead and submit it to our free directory here https://1mdr.short.gy/submityoursite