Posts

Showing posts from September 19, 2015

Roll up Contact on Account

trigger updateContactCountonAccount on Contact (after insert,after Delete) { Set<ID> setofAccId = new Set<Id>(); Map<Id,List<Contact>> MapAccIDTOListOfCont = new Map<Id,List<Contact>>(); Map<ID,Account> MapIDToAccount = new Map<Id,Account>(); if(Trigger.isAfter && Trigger.isInsert) { for(Contact ObjCOn: trigger.new) { if(ObjCOn.AccountId !=null) { setofAccId.add(ObjCOn.AccountId); } } } if(Trigger.isAfter && Trigger.isDelete) { for(Contact ObjCOn: trigger.old) { if(ObjCOn.AccountId !=null) { setofAccId.add(ObjCOn.AccountId); } } } for(Account objAcc:[Select Id from Account where ID IN:setofAccId]) { MapIDToAccount.put(objAcc.ID,objAcc); } for(Contact objCon:[Select id , Name,AccountId from Contact where Accou