You need to sign in to do that
Don't have an account?
m@x~W0W~
Error: Compile Error: Expression cannot be assigned at line -1 column -1
trigger updateContactCount on Contact (after insert, after delete) { Set<id> accIds = new Set<id>(); for (Contact a : Trigger.new) { accIds.add(a.Account.id); } Map<Id, Integer> accConMap = new Map<Id, Integer>(); for(AggregateResult ar : [SELECT Count(id) cnt, AccountId accId FROM Contact WHERE AccountId IN :accIds Group By AccountId] ) { accConMap.put(String.valueOf(ar.get('accId')), Integer.valueOf(ar.get('cnt'))); } // Map<id, Integer> cnt = new Map<id, Integer>([Select count() from Contact where Account.id in: accIds]); for(id a : accIds) { Account.CountContact__c = accConMap.get(a); } }
Please suggest.
The context variable Trigger.new is only available in "after insert" and "after update" triggers. Check out the "Trigger context variables" section in the Apex Developer's Guide. You probably want to use System.Trigger.old for the "after delete" case
Please compare if the action is isdelete or isinsert and depending on the response - use trigger.new or trigger.old