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
raman123raman123 

how can i write test class for change of case ownwer and email class

trigger Trigger_case_Send_Email on case (after update) {
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();   
        Set<Id> ownerIds = new Set<Id>();
   
        for (case mycase : Trigger.new) {

        case oldcon = Trigger.oldMap.get(mycase.Id);
        
    
        if (mycase.ownerid != oldcon.ownerid ) {

        ownerids.add(oldcon.ownerid) ; 

 }
    
        Map<Id, User> userMap = new Map<Id,User>([select Name, Email from User where Id in :ownerIds]);
        for(case cas : Trigger.old)
    {
        User theUser = userMap.get(cas.ownerId);
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new string []{theuser.Email};
        mail.setToAddresses(toAddresses);   
        mail.setSubject('A case owned by you has been transfer');   
        string mailBody = 'Link: '+ URL.getSalesforceBaseUrl().toExternalForm()+ '/'+Cas.Id;
        mail.setPlainTextBody(mailBody);
        mails.add(mail);
        
            
    }
   Messaging.SendEmail(mails);
   }
   }
Raj VakatiRaj Vakati
100 % ... use this code
@isTest
private class CaseSendEmailTestClass {
    
    static testMethod void testCC () {
        
        
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        System.runAs(uu){
            
            //create account
            Account acc = new Account();
            //enter details  
            acc.Name = 'Test Account';
            insert acc;
            
            //create case
            Case c = new Case();
            //enter details
            c.AccountId = acc.Id;
            c.Type = 'My Type';
            c.Origin = 'My Origin';
            c.Status = 'My Status';
            insert c;
        }
        Case c =[Select Id ,Status from Case Limit 1 ] ; 
        c.Status='Closed' ;
        c.OwnerId =UserInfo.getUserId();
        update c ;
    }
    
}