+ Start a Discussion
golla Anithagolla Anitha 

Create a custom field called 'Count of Cases' on Contact record. This should contain the count of associated cases on the Contact. In the event of deletion of a Case, this count field should be updated appropriately on the Contact. Also, Test class should

Create a custom field called 'Count of Cases' on Contact record. This should contain the count of associated cases on the Contact. In the event of deletion of a Case, this count field should be updated appropriately on the Contact. Also, Test class should be written. 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class contactcase {
    public static void contactcasemethod(List<case> lstcase)
    {
        List<id> lstid=new List<id>();
        for(case c:lstcase)
    {
        lstid.add(c.ContactId);
         system.debug('lstid'+lstid);
        
    }
        integer noOfRecInCase=lstcase.size();
        system.debug('noOfRecInCase'+noOfRecInCase);
        //fetch contact record to update if exists
      List <contact> replica=[select id,Count_of_Cases__c from contact where id in :lstid ];
        system.debug('fetch contact record to update if exists'+replica);
        if(replica.size()>0)
        {
        List<integer> casecount=[SELECT count(id) FROM case WHERE contactid in:replica];
        
        //logic for inserting new case, update contact count
         
        for(contact replica3:replica)
       {
           if(casecount[0]>0 || casecount[0]!=null)
           {
       replica3.Count_of_Cases__c=casecount[0]+noOfRecInCase;
           system.debug('logic for inserting new case, update contact count'+replica3);
           }
        else{
            replica3.Count_of_Cases__c=noOfRecInCase;
             system.debug('logic for inserting new case, update contact count1'+replica3);
        }
       }
        }
        update replica;
    }
}
   /* public static void contactcasemethod1(List<case> lstcase1)
    {
        List<id> lstid1=new List<id>();
        for(case c1:lstcase1)
    {
        lstid1.add(c1.ContactId);
         system.debug('lstid'+lstid1);
        
    }
        integer noOfRecInCase1=lstcase1.size();
        //fetch contact record to update if exists
      List <contact> replic=[select id,Count_of_Cases__c from contact where id in :lstid1 ];
        system.debug('fetch contact record to update if exists'+replic);
        if(replic.size()>0)
        {
        List<case> casecount=[SELECT Origin, Subject, status, ContactId FROM case WHERE contactid in:replic];
        system.debug('casecount'+casecount.size());
        //logic for inserting new case, update contact count
        for(contact replica3:replic)
       {
           if(casecount.size()>0 || casecount.size()!=null)
           {
       replica3.Count_of_Cases__c=casecount.size()-noOfRecInCase1;
           system.debug('logic for inserting new case, update contact count'+replica3);
           }
        else{
            replica3.Count_of_Cases__c=noOfRecInCase1;
             system.debug('logic for inserting new case, update contact count1'+replica3);
        }
       }
        }
        update replic;
    }
}*/
error was like
Illegal assignment from List<AggregateResult> to List<Integer>
AnkaiahAnkaiah (Salesforce Developers) 
Hi Anitha,

Please try with below code.
 
trigger CaseCountOnContact on Case (after insert, after update, after delete, after undelete) {
    Map<Id, List<Case>> mapContactIdCaseList = new Map<Id, List<Case>>();
    Map<Id, List<Case>> mapContactIdDelCaseList = new Map<Id, List<Case>>();
    Set<Id> ConIds = new Set<Id>();    
    List<Contact> ContactList = new List<Contact>();
    
    if(trigger.isInsert) {
        for(Case cs : trigger.New) {
            if(String.isNotBlank(cs.ContactId)) {
                if(!mapContactIdCaseList.containsKey(cs.ContactId)) {
                    mapContactIdCaseList.put(cs.ContactId, new List<Case>());
                }
                mapContactIdCaseList.get(cs.ContactId).add(cs); 
                ConIds.add(cs.ContactId);
            }   
        }  
    }
    
    if(trigger.isUpdate) {
        for(Case cs : trigger.New) {
            if(String.isNotBlank(cs.ContactId) && cs.ContactId != trigger.oldMap.get(cs.Id).ContactId) {
                if(!mapContactIdCaseList.containsKey(cs.ContactId)){
                    mapContactIdCaseList.put(cs.ContactId, new List<Case>());
                }
                mapContactIdCaseList.get(cs.ContactId).add(cs); 
                ConIds.add(cs.ContactId);
            } else if(String.isBlank(cs.ContactId) && String.isNotBlank(trigger.oldMap.get(cs.Id).ContactId)) {
                if(!mapContactIdDelCaseList.containsKey(cs.ContactId)){
                    mapContactIdDelCaseList.put(cs.ContactId, new List<Case>());
                }
                mapContactIdDelCaseList.get(cs.ContactId).add(cs);   
                ConIds.add(trigger.oldMap.get(cs.Id).ContactId);
            }
        }  
    }
    
    if(trigger.isUndelete) {
        for(Case cs : trigger.new) {
            if(String.isNotBlank(cs.ContactId)){
                if(!mapContactIdCaseList.containsKey(cs.ContactId)){
                    mapContactIdCaseList.put(cs.ContactId, new List<Case>());
                }
                mapContactIdCaseList.get(cs.ContactId).add(cs);     
                ConIds.add(cs.ContactId);
            }
        }  
    }      

    if(trigger.isDelete) {
        for(Case cs : trigger.Old) {
            if(String.isNotBlank(cs.ContactId)){
                if(!mapContactIdDelCaseList.containsKey(cs.ContactId)){
                    mapContactIdDelCaseList.put(cs.ContactId, new List<Case>());
                }
                mapContactIdDelCaseList.get(cs.ContactId).add(cs);    
                ConIds.add(cs.ContactId); 
            }
        }  
    }   
    
    if(ConIds.size() > 0) {
        ContactList = [SELECT Id, Number_of_Cases__c FROM Contact WHERE Id IN : ConIds];
        
        for(Contact Con : ContactList) {
            Integer noOfcases = 0;
            if(mapContactIdCaseList.containsKey(Con.Id)) {
                noOfcases += mapContactIdCaseList.get(Con.Id).size();
            }
            if(mapContactIdDelCaseList.containsKey(Con.Id)) {
                noOfcases -= mapContactIdDelCaseList.get(Con.Id).size();
            }
            Con.Number_of_Cases__c = Con.Number_of_Cases__c == null ? noOfcases : (Con.Number_of_Cases__c + noOfcases);
        }
        
        update ContactList;    
    }
}

If this helps, Please mark it as best answer.

Regards,
Ankaiah Bandi
golla Anithagolla Anitha
thank you Ankaiah bandi.