Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement