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
Snehal Gaware 15Snehal Gaware 15 

test class for after update trigger on case

Hi team,
I need help in writting test class for after update trigger on case.
trigger caseOwnerUpdate on Case (after update) {
    List<Case> updateCS = new List<Case>();
    Map<Id,Case> cases = new Map<Id,Case>();
    
    for (Case cs : Trigger.new)
    {
        if(Trigger.isUpdate) {  
            System.debug('>>>>> Owner ID: '+cs.ownerId+' Temp Owner ID: '+cs.TempOwnerId__c);
            if(cs.TempOwnerId__c <> null && cs.TempOwnerId__c <> '' && cs.OwnerId != trigger.oldMap.Get(cs.Id).OwnerId) {
                system.debug('pleasecheck'+trigger.oldMap.Get(cs.Id).OwnerId);
                if(cs.OwnerId <> cs.TempOwnerId__c) {
                    cases.put(cs.id,cs);
                }
            }           
        }   
    }
    if (cases.isEmpty()) return;
    
    for (Case cs : [SELECT OwnerId,TempOwnerId__c FROM Case WHERE id in :cases.keySet()]) {
        cs.OwnerId = cases.get(cs.Id).TempOwnerId__c;
        system.debug('pleasecheck2'+ cs.OwnerId);
        cs.TempOwnerId__c = 'SKIP'; //flag to stop infinite loop upon update
        updateCS.add(cs);
    }
    System.debug('>>>>>Update Cases: '+updateCS);
    
    //
    //Update last assignment for Assignment Group in batch
    //
    if (updateCS.size()>0) {
        try {
            update updateCS;
        } catch (Exception e){

        }
    }
}

Thanks in advance. Any help will be appreciated.
Best Answer chosen by Snehal Gaware 15
Raj VakatiRaj Vakati
try this
@isTest
public class CaseOwnerUpdateTest {
	
    static testmethod void testCase1(){
       	
		   
        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.runAss(uu){
        
		
		Account a = new Account();
        a.Name = 'Test';
        a.Industry = 'Retail';
        insert a;
		  Contact c=new Contact(
            FirstName='fname',
            LastName = 'lname',accountid =a.id , 
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert c; 
        
		Case caseObj = new Case(
		ContactId = c.id,
		AccountId = a.Id,
		Status = 'Working',TempOwnerId__c  = uu.Id ,
		Origin = 'Phone');

		insert caseObj;

		}
		
		 User uu1=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 uu1;
		Case c1 = [Select Id ,OwnerId from Case Limit 1 ]; 
		c1.OwnerId =uu1.Id ; 
		update c1 ;
		
    }
}

 

All Answers

Raj VakatiRaj Vakati
Use this
 
@isTest
public class CaseOwnerUpdateTest {
	
    static testmethod void testCase1(){
       	
		   
        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.runAss(uu){
        
		
		Account a = new Account();
        a.Name = 'Test';
        a.Industry = 'Retail';
        insert a;
		  Contact c=new Contact(
            FirstName='fname',
            LastName = 'lname',accountid =a.id , 
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert c; 
        
		Case caseObj = new Case(
		ContactId = c.id,
		AccountId = a.Id,
		Status = 'Working',
		Origin = 'Phone');

		insert caseObj;

		}
		
		 User uu1=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 uu1;
		Case c1 = [Select Id ,OwnerId from Case Limit 1 ]; 
		c1.OwnerId =uu1.Id ; 
		update c1 ;
		
    }
}

 
Snehal Gaware 15Snehal Gaware 15
Hi Raj,
It is only giving coverage as 41%.

User-added image
Raj VakatiRaj Vakati
What is the data type of TempOwnerId field .. please pass that value ..
Snehal Gaware 15Snehal Gaware 15
Hi Raj,
It is text field, i have passed value still showing coverage as 41.
Raj VakatiRaj Vakati
try this
@isTest
public class CaseOwnerUpdateTest {
	
    static testmethod void testCase1(){
       	
		   
        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.runAss(uu){
        
		
		Account a = new Account();
        a.Name = 'Test';
        a.Industry = 'Retail';
        insert a;
		  Contact c=new Contact(
            FirstName='fname',
            LastName = 'lname',accountid =a.id , 
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert c; 
        
		Case caseObj = new Case(
		ContactId = c.id,
		AccountId = a.Id,
		Status = 'Working',TempOwnerId__c  = uu.Id ,
		Origin = 'Phone');

		insert caseObj;

		}
		
		 User uu1=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 uu1;
		Case c1 = [Select Id ,OwnerId from Case Limit 1 ]; 
		c1.OwnerId =uu1.Id ; 
		update c1 ;
		
    }
}

 
This was selected as the best answer
Snehal Gaware 15Snehal Gaware 15
Thanks raj. it worked as expected.
Could you please help me with following trigger test class.
trigger TestedStatus on SC_Problem_Management__c (Before Update) 
{
 for(SC_Problem_Management__c c : Trigger.new)
        {   System.debug('testing');
            if(c.Related_Incident_Count__c == c.All_child_with_tested_status__c)
            {
                c.Status__c ='Tested';
               
            }
         System.debug('//' + c.Status__c);
         if(c.Related_Incident_Count__c == c.All_child_with_reopen_status__c)
            {
                c.Status__c ='ReOpen';
               
            }
         if(c.Related_Incident_Count__c == c.all_child_with_closed_status__c)
            {
                c.Status__c ='Closed';
               
            }
        }	
}

 
Raj VakatiRaj Vakati
Use this
 
@isTest
public class SC_Problem_Management {
	
    static testmethod void testCase1(){
       	
		   Account a = new Account();
        a.Name = 'Test';
       // a.Industry = 'Retail';
        insert a;
		  
         Contact c=new Contact(
            FirstName='fname',
            LastName = 'lname',
            accountid =a.id , 
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert c; 
        
         // Add all fields to case 
		Case caseObj = new Case(
		ContactId = c.id,
		AccountId = a.Id
		);

		insert caseObj;

		SC_Problem_Management__c pm = new SC_Problem_Management__c();
		pm.Products__c= 'AutoClass';
        pm.Impact__c='Critical';
        pm.Urgency__c='Critical';
        pm.Priority__c='Critical';
		pm.Related_Incident_Count__c ='1';
		pm.Case__c = caseObj.Id ;
		// add all fields 
		insert pm ; 
       pm.All_child_with_tested_status__c='1';
	   update pm ; 
	   
		
		SC_Problem_Management__c pm1 = new SC_Problem_Management__c();
		pm1.Products__c= 'AutoClass';
        pm1.Impact__c='Critical';
        pm1.Urgency__c='Critical';
        pm1.Priority__c='Critical';
		pm1.Related_Incident_Count__c ='1';
		pm1.Case__c = caseObj.Id ;
		// add all fields 
		insert pm1 ; 
       pm1.All_child_with_reopen_status__c='1';
	   update pm1 ;

SC_Problem_Management__c pm11 = new SC_Problem_Management__c();
		pm11.Products__c= 'AutoClass';
        pm11.Impact__c='Critical';
        pm11.Urgency__c='Critical';
        pm11.Priority__c='Critical';
		pm11.Related_Incident_Count__c ='1';
		pm11.Case__c = caseObj.Id ;
		// add all fields 
		insert pm11 ; 
       pm11.all_child_with_closed_status__c='1';
	   update pm11 ;	   
	   
    }
}

 
Snehal Gaware 15Snehal Gaware 15
Hi Raj,
I am getting error as "Field is not writeable: SC_Problem_Management__c.Related_Incident_Count__c" and "Field is not writeable: SC_Problem_Management__c.All_child_with_tested_status__c".

As these fields are roll-up summary fields. 
Raj VakatiRaj Vakati
Rollup summer to case ?? 

Please insert the data like below 
 
// Add all fields to case 
		Case caseObj = new Case(
		ContactId = c.id,
		AccountId = a.Id
		);

		insert caseObj;

		SC_Problem_Management__c pm = new SC_Problem_Management__c();
		pm.Products__c= 'AutoClass';
        pm.Impact__c='Critical';
        pm.Urgency__c='Critical';
        pm.Priority__c='Critical';
		pm.Related_Incident_Count__c ='1';
		pm.Case__c = caseObj.Id ;
		// add all fields 
		insert pm ; 
       pm.All_child_with_tested_status__c='1';
	   update pm ;

 
Snehal Gaware 15Snehal Gaware 15
rollup summary on SC_Problem_Management__c (Parent object) and SC_Problem_Case_Link__c(child object).
Snehal Gaware 15Snehal Gaware 15
Hi Raj, I have resolved the issue by creating child records and it gave 100% code coverage.
Thank you so much for your help.