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
Lokesh PatilLokesh Patil 

Can someone write Test class for this trigger

Is code right

Trigger UpdateCaseCount on Case(After Insert, After Update) {
    Set<id> AccIdUpd = new Set<Id>();
    Map<Id, Id> AccPrd = new Map<Id, Id>();
    set<id> ContactId = new set<id>();
 
  //Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails =
  new List<Messaging.SingleEmailMessage>();
 
    
    for(Case cs:Trigger.New) {                
        if(Trigger.IsInsert || (cs.IsClosed && Trigger.OldMap.get(cs.Id).IsClosed != cs.IsClosed)) {
            AccIdUpd.add(cs.AccountId);
            AccPrd.put(cs.AccountId, cs.Product__c);
        }
    
    }      

    List<AggregateResult> ARListCurrIssue = [select AccountId, Product__c, Count(Id) CaseCount from Case where 
                                             AccountId In:AccIdUpd and IsClosed != True group By AccountId, Product__c];
    
    // Logic to update curernt Issues
    List<Account> UpdAccList = new List<Account>();
    for(AggregateResult AR : ARListCurrIssue) {
    String CurrCount = AR.get('CaseCount') + ', ' +  AccPrd.get((ID)AR.get('Product__c'));
        UpdAccList.add(new Account(Id = (ID)AR.get('AccountId'), CurrentCount__c = CurrCount));
        Integer iCount = Integer.valueOf(AR.get('CaseCount'));
        If(iCount == 5) {
            //write logic to sendMail      
  
         Contact con = [Select firstname,lastname,email,id,name,MobilePhone from Contact where id in :ContactId];
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
   
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(con.email);
      mail.setToAddresses(sendTo);
   
      // Step 3: Set who the email is sent from
      mail.setReplyTo('lokeshpatil830@gmail.com');
      mail.setSenderDisplayName('lokesh patil');
   
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('puja.patil@aress.com');
      mail.setCcAddresses(ccTo);
 
      // Step 4. Set email contents - you can use variables
      mail.setSubject('Get in touch with the service team');
      String body = 'Dear ' + con.FirstName;
     
      mail.setHtmlBody(body);
   
      // Step 5. Add your email to the master list
      mails.add(mail);
        Messaging.sendEmail(mails);              
        
    }

}    
    if(UpdAccList.size()>0){ 
        update UpdAccList;

}
    // Logic to update Past Issues
      ARListCurrIssue = [select AccountId, Count(Id) CaseCount  from Case where AccountId In:
                                             AccIdUpd and IsClosed = True group By AccountId];
    
    UpdAccList = new List<Account>();
    for(AggregateResult AR : ARListCurrIssue) {
        String PastCount = AR.get('CaseCount') + ', ' +  AccPrd.get((ID)AR.get('AccountId'));
        UpdAccList.add(new Account(Id = (ID)AR.get('AccountId'), PastCount__c = PastCount));
        
    }
        
    if(UpdAccList.size()>0) {
        update UpdAccList;
    }

}
 
AbhishekAbhishek (Salesforce Developers) 
https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test

The above developer discussion will guide you on how to write test class, Go through it.


I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.