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
mirza OSAMAmirza OSAMA 

/* No of contacts field on account object=total contacts associated to account * insert,update works but not delete*/

/* No of contacts field on account=total contacts associated to account
 * insert,update works but not delete*/

public class mirza {
       
    public static void method1(List<Contact>newlist,list<Contact>oldlist){
      
        set<id>conset=new set<id>();
       try{
        for(Contact c: newlist){
            if(c.AccountId!=null){
                conset.add(c.Accountid);
            }
        }
        if(oldlist!=null){
            for(Contact c:oldlist){
                conset.add(c.AccountId);
            }
        }
        if(conset!=null){
            list<Account> acclist =[select id,No_of_Contacts__c,(select id from Contacts)from Account where id in :conset];
            if(acclist!=null){
            for(Account acc:acclist){
                acc.No_of_Contacts__c=acc.Contacts.Size();
            }
            }
            if(!acclist.isEmpty()){
                Update acclist;
            }
            }
       } Catch(ExCEPtion e){
        System.debug(e.getMESSAGE());
        }
       
    }
}
------------------
trigger mirzatrigger on Contact (after insert,after update,after delete, after undelete) {
    if(Trigger.isInsert || Trigger.isupdate || Trigger.isdelete || Trigger.isUndelete){
        mirza.method1(Trigger.new,Trigger.old);
    }
}
PriyaPriya (Salesforce Developers) 
Hey Mirza,

What is the error occuring while deleting the contact record?
Did you check the debug log ?

Thanks!
mirza OSAMAmirza OSAMA
No error Priya.Its in debug log i tried to print some arbitary name in b/w class to venfy .... that too its not printing...
mirza OSAMAmirza OSAMA
the soql is even not executing
mirza OSAMAmirza OSAMA
working fine for insert,update isuue with delete in debug logg attempt to derefrence null object