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
Raja JammulaRaja Jammula 

When a contact is inserted or deleted then update "Custom Field" on Account .

I am New to apex development:

this is my trigger:

trigger ContactTrigger on Contact(after delete, after insert, after undelete, after update, before delete, before insert, before update) {

  ContactTriggerHandler handler = new ContactTriggerHandler();

  /* Before Insert */
  if(Trigger.isInsert && Trigger.isBefore){
   // handler.OnBeforeInsert(Trigger.new);
  }
  
  /* After Insert */
  else if(Trigger.isInsert && Trigger.isAfter){
    handler.OnAfterInsert(Trigger.new);
  }
  
  /* Before Update */
  else if(Trigger.isUpdate && Trigger.isBefore){
    //handler.OnBeforeUpdate(Trigger.old, Trigger.new, Trigger.newMap, Trigger.oldMap);
  }
  
  /* After Update */
  else if(Trigger.isUpdate && Trigger.isAfter){
   // handler.OnAfterUpdate(Trigger.old, Trigger.new, Trigger.newMap, Trigger.oldMap);
  }
  
  /* Before Delete */
  else if(Trigger.isDelete && Trigger.isBefore){
    //handler.OnBeforeDelete(Trigger.old, Trigger.oldMap);
  }
  
  /* After Delete */
  else if(Trigger.isDelete && Trigger.isAfter){
    handler.OnAfterDelete(Trigger.old, Trigger.oldMap);
  }

  /* After Undelete */
  else if(Trigger.isUnDelete){
    //handler.OnUndelete(Trigger.new);
  }

}

this is my class:

public without sharing class ContactTriggerHandler { 
   
    public void OnBeforeInsert(Contact[] newContacts) {
        // BEFORE INSERT LOGIC
    
    }
    
    public void OnAfterInsert(Contact[] newContacts) {
        // AFTER INSERT LOGIC
         insertcontact(newContacts, new Map<Id,Contact>());
    }
    
    public void OnBeforeUpdate(Contact[] oldContacts, Contact[] updatedContacts, Map<ID, Contact> newMap, Map<ID, Contact> oldMap) {
        // BEFORE UPDATE LOGIC
             
    }
    
    public void OnAfterUpdate(Contact[] oldContacts, Contact[] updatedContacts, Map<Id, Contact> newMap, Map<Id, Contact> oldMap) {
        // AFTER UPDATE LOGIC
        
    }
    
    public void OnBeforeDelete(Contact[] contactsToDelete, Map<ID, Contact> oldMap) {
        // BEFORE DELETE LOGIC
           //Deletecontact(contactsToDelete);
    }
    
    public void OnAfterDelete(Contact[] deletedContacts, Map<ID, Contact> oldMap) {
        // AFTER DELETE LOGIC
        //Deletecontact(deletedContacts);
        insertcontact(deletedContacts, oldMap);
    }
    
    public void OnUndelete(Contact[] restoredContacts) {
        // AFTER UNDELETE LOGIC
        
    }
    
    public void insertcontact(Contact[] Contacts, Map<ID,Contact> oldMap ){
        // List<Contact> contactList = [SELECT ID, account.Contact_Count__c FROM Contact];
        set<Id> accid = new set<Id>();
        //List<Contact> contactList = new List<Contact>();
        for(contact con: Contacts){
            accid.add(con.accountid);
        }
        
       for(contact cons: oldMap.values()){
            accid.add(cons.accountId);
        }
        
           List <Account> accList = new List <Account>();
        
        //if(newMap.get(con.Id)!=oldMap.get(con.Id)
        /* Aggregate Query */
        for(Account acc:[SELECT Id,Name,Contact_Count__c,(Select Id from Contacts) from Account where Id IN: accid]){
            if(acc!=null){
            Account accObj = new Account ();
            accObj.Id = acc.Id;
            accObj.Contact_Count__c = acc.Contacts.size();
            system.debug('accsize:--->' +acc.Contacts.size() );
            accList.add(accObj);
        }
            else{
             Account ccObj = new Account ();
                ccObj.Id = acc.Id;
             ccObj.Contact_Count__c = 0;
             accList.add(ccObj);
            }
        }
        update accList;
    }
    
     public static void Deletecontact(Contact[] deletedContacts){
         
         set<Id> accid = new set<Id>();
         for(contact con: deletedContacts){
             accid.add(con.accountId);
         }
         
         List <Account> accList = new List <Account>();
         
         //if(newMap.get(con.Id)!=oldMap.get(con.Id)
         // Aggregate Query
         for(Account acc:[SELECT Id,Name,Contact_Count__c,(Select Id from Contacts) from Account where Id IN: accid]){
             system.debug('acclist:--->' +acc );
             Account accObj = new Account ();
             accObj.Id = acc.Id;
             system.debug('accsize:--->' +acc.Contacts.size() );
             accObj.Contact_Count__c = acc.Contacts.size();
             system.debug('accsize:--->' +acc.Contacts.size() );
             accList.add(accObj);
         }
            system.debug('accList:--->' +accList);
         update accList;
     }
}

when i try to insert contact the field is getting updated but when i delete it it's not getting updated.
can anyone help me where i am going wrong.

 
Best Answer chosen by Raja Jammula
Mubeen QawiMubeen Qawi
Check some of the discussions here: I reckon the issue is with Trigger context AfterDelete. I maybe wrong.

Trigger Context Variables: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
The context variable Trigger.new: https://developer.salesforce.com/forums/?id=906F00000008vKwIAI

All Answers

Raja JammulaRaja Jammula
can anyone help me on how to prepare or learn building logic in the code
 
Mubeen QawiMubeen Qawi
Check some of the discussions here: I reckon the issue is with Trigger context AfterDelete. I maybe wrong.

Trigger Context Variables: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
The context variable Trigger.new: https://developer.salesforce.com/forums/?id=906F00000008vKwIAI
This was selected as the best answer