Number fields on Account : "No of Contacts" Write trigger to populate/update field on Account record whenever a new Contact record is inserted or deleted.
-- whenever i am deleting last contact record it is not updating Account field from 1 to 0. Value should be updated to 0 or Blank
trigger UpdateContactCountOnAccount on Contact (after insert, after update, after delete)
{
Set<Id> AccountIds = new Set<Id>();
if(!Trigger.isDelete)
{
for (Contact ct : Trigger.new)
{
if(Trigger.isInsert && ct.AccountId != null)
{
AccountIds.add(ct.AccountId);
}
if(Trigger.isUpdate)
{
if(ct.AccountId==null && Trigger.oldMap.get(ct.Id).AccountId != null)
{
AccountIds.add(Trigger.oldMap.get(ct.Id).AccountId);
}
if(ct.AccountId!=null && Trigger.oldMap.get(ct.Id).AccountId != null
&& ct.AccountId != Trigger.oldMap.get(ct.Id).AccountId)
{
AccountIds.add(ct.AccountId);
AccountIds.add(Trigger.oldMap.get(ct.Id).AccountId);
}
if(ct.AccountId!=null && Trigger.oldMap.get(ct.Id).AccountId == null)
{
AccountIds.add(ct.AccountId);
}
}
}
}
else
{
for (Contact ct : Trigger.old)
{
if(Trigger.isDelete && ct.AccountId != null)
{
AccountIds.add(ct.AccountId);
}
}
}
List<Account> AcctToUpdate = new List<Account>();
for (AggregateResult ar: [Select Count(Id) ContactCount, AccountId
from Contact where AccountId IN: AccountIds GROUP BY AccountId])
{
Account tmp = new Account(Id=(Id)ar.get('AccountId'), No_of_Contacts__c=(Decimal)ar.get('ContactCount'));
AcctToUpdate.add(tmp);
}
if(AcctToUpdate.size()>0)
update AcctToUpdate;
}
All is working fine but whenever i am deleting last contact record it is not updating Account field from 1 to 0 Can someone help me