function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
m@x~W0W~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. 

Best Answer chosen by m@x~W0W~
Sonam_SFDCSonam_SFDC

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