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
Mayur TripathiMayur Tripathi 

write a test class for case closedd log

trigger CaseClosedLog on Case (before insert, before update,after update) {
    
    
    
    
    if(Trigger.IsBefore)
{
    
        List<Ownership_Log__c> OwnershipLst = new List<Ownership_Log__c>();        
                
                boolean isUpdateCase = false;
        
            for(Case c : Trigger.New){
                
                system.debug(c.Owner.Name);
                if(Trigger.IsUpdate)
                {
                    
                    Case oldc = Trigger.oldMap.get( c.id );
                    
                    
                    
                    Case caseOwner  =  [select Owner.Name from  Case where Id =: c.id limit 1];
                    
                    
                    if(oldc.Status == 'Closed' && oldc.Status != c.status ){
                        List<Ownership_Log__c> listOwnershiplogs  =  [select Id,Sequence__c,End__c,Owner__c from  Ownership_Log__c 
                                where Sequence__c =: c.Ownership_Sequence__c  and Case__c =: c.id limit 1];
                        
                        if(caseOwner.Owner.Name !=  listOwnershiplogs[0].Owner__c)
                        {
                            Ownership_Log__c ownership = new Ownership_Log__c();
                         ownership.Case__c = c.id;
                         ownership.Owner__c = caseOwner.Owner.Name;
                         
                         ownership.Sequence__c = c.Ownership_Sequence__c + 1;        
                         ownership.Start__c    = system.now();
                         ownership.Type__c    = 'Owner Change';
                         OwnershipLst.add(ownership);
                        
                             insert ownership;
                        
                         
                         c.Ownership_Sequence__c = c.Ownership_Sequence__c + 1;
                         
                        }
                        
                    }
                    
                    if( oldc.OwnerId != c.OwnerId && c.status <> 'Closed')
                    {
                        
                        List<Ownership_Log__c> listOwnershiplogs  =  [select Id,Sequence__c,End__c from  Ownership_Log__c where Sequence__c =: c.Ownership_Sequence__c  and Case__c =: c.id limit 1];
                            
                            if(listOwnershiplogs[0].End__c == null)
                            {
                                 listOwnershiplogs[0].End__c = system.now();
                                 system.debug('listOwnershiplogs---' + listOwnershiplogs[0]);
                                  update listOwnershiplogs[0];
                            }                 
                        
                         Ownership_Log__c ownership = new Ownership_Log__c();
                         ownership.Case__c = c.id;
                         ownership.Owner__c = caseOwner.Owner.Name;
                         
                         ownership.Sequence__c = c.Ownership_Sequence__c + 1;        
                         ownership.Start__c    = system.now();
                         ownership.Type__c    = 'Owner Change';
                         OwnershipLst.add(ownership);
                         
                         insert ownership;
                         
                         
                        
                       
                     
                         c.Ownership_Sequence__c = c.Ownership_Sequence__c + 1;
                     
                         
                        
                         
                         
                         
                         system.debug('OwnershipLst---' + OwnershipLst);
                    }
                    system.debug('Trigger333---' + c);
                    if(c.Status == 'Closed')
                    {
                        
                         
                         if(OwnershipLst.size() > 1)
                         {
                             OwnershipLst[0].End__c = system.now();
                              update OwnershipLst[0];
                         }
                         else
                         {
                             
                             
                                     List<Ownership_Log__c> listOwnershiplogs  =  [select Id,Sequence__c,End__c from  Ownership_Log__c where Sequence__c =: c.Ownership_Sequence__c and Case__c =: c.id limit 1];
                                 if(listOwnershiplogs[0].End__c == null){
                                     listOwnershiplogs[0].End__c = system.now();
                                      update listOwnershiplogs[0];
                                 }
                             
                         }
                         isUpdateCase = true;
                         
                          
                    } 
                }
                if(Trigger.IsInsert )
                {
                    Case caseOwner  =  [select Owner.Name from  Case where Id =: c.id limit 1];
                     Ownership_Log__c ownership = new Ownership_Log__c();
                     ownership.Case__c = c.id;
                     ownership.Owner__c = caseOwner.Owner.Name;
                     ownership.Sequence__c = 1;        
                     ownership.Start__c    =  system.now();
                     ownership.Type__c    = 'Owner Change';
                     
                         c.Ownership_Sequence__c = 1;
            
                    update ownership;
            
                         
                    if(c.Status == 'Closed'){
                    
                        
                         if(OwnershipLst.size() > 1)
                         {
                             OwnershipLst[0].End__c = system.now();
                         }
                         
                         c.Ownership_Sequence__c = c.Ownership_Sequence__c + 1;
                         
                         
                            
                        
                    }
                }
                
                
            
            
            }
        
    }
    
    
        
    if(Trigger.IsAfter)
    {
        for(Case c : Trigger.New){
            Case caseOwner  =  [select Owner.Name from  Case where Id =: c.id limit 1];
            if(c.Status != 'Closed')
            {
                List<Ownership_Log__c> listOwnershiplogs  =  [select Id,Sequence__c,End__c,Owner__C from  Ownership_Log__c where Sequence__c =: c.Ownership_Sequence__c  and Case__c =: c.id limit 1];
                listOwnershiplogs[0].Owner__C = caseOwner.Owner.Name;
                Update listOwnershiplogs;
                
            }
        }
    }        
     
}


This is urgent 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Mayur Tripathi ,
 
  • Do You got a chance to check the Test generator app from App Exchange please check the below link
https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000EFozgUAD

Hope it will be helpful.

Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar