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
coolkrishcoolkrish 

After update trigger code coverage

Hi,
I am not getting code coverage for the trigger lines below in bold:
Any help appreciated.

trigger RR_Terr_Inactive_After_Ins_Upd on RRTerritory__c (after insert,after update) {

    List<Id> TerrIds = new List<Id>();
    
    if(Trigger.isUpdate){
        for (RRTerritory__c ter : Trigger.New) { 
        RRTerritory__c  oldTer = Trigger.Oldmap.Get(ter.Id);
        
        if (ter.Status__c == 'Inactive' && oldTer.Status__c != ter.Status__c){         
            TerrIds.add(ter.Id);            
        }  
            }        
                }    
 
    if(TerrIds != null && !TerrIds .isEmpty()) {         
        
        List<RREmployee_Territory__c> empTerList = new List<RREmployee_Territory__c>([SELECT Id,End_Date__c FROM RREmployee_Territory__c WHERE Territory__c     IN:TerrIds AND Active__c = TRUE]);        
        List<RREmployee_Territory__c> empTerList1 = new List<RREmployee_Territory__c>();
        
        for (RRTerritory__c  ter : Trigger.New){         
            for(RREmployee_Territory__c et: empTerList) {          
                     et.Active__c = FALSE;                 
                       et.End_Date__c = ter.Effective_End_Date__c;  
                 
                    empTerList1.add(et);            

            }
        }        
         
        if(empTerList1 != null && !empTerList1.isEmpty()){
                update empTerList1;
        }   
       
    }
}

Test class:

@isTest
public class RR_Territory_Inactive_Test {    
    public static testMethod void runTestCases() { 
        try  {           
            
            //Insert Territory
            RRTerritory__c terr1 = new RRTerritory__c();
            terr1.Territory_ID__c='123';
            terr1.Name = 'New Territory';
            terr1.Franchise__c = 'franc1';
            terr1.Effective_Start_Date__c = System.today().addDays(-10);
            terr1.Status__c = 'Active';
            insert terr1;
            System.assertEquals('New Territory', terr1.Name);     
            
            //Insert RREmployee__c     
            RREmployee__c emp = new RREmployee__c();
            emp.First_Name__c='Test';
            emp.Last_Name__c='Emp';
            emp.Name = 'Test emp';
            emp.Franchise__c = 'test franchise';
            emp.Territory_ID__c ='100'; 
            insert emp;
            System.assertEquals('Test emp', emp.Name);            
                        
            terr1.Status__c = 'Inactive';         
            terr1.Effective_End_Date__c = System.today().addDays(1);
            update terr1;
       
        }catch(DmlException e){
            System.debug('Exception Occurred: '+ e.getMessage());            
        }        
    }    

}
Raj VakatiRaj Vakati
You need to insert RREmployee_Territory__c  object into the test class as shown below  
 
@isTest
public class RR_Territory_Inactive_Test {    
    public static testMethod void runTestCases() { 
        try  {           
            
            //Insert Territory
            RRTerritory__c terr1 = new RRTerritory__c();
            terr1.Territory_ID__c='123';
            terr1.Name = 'New Territory';
            terr1.Franchise__c = 'franc1';
            terr1.Effective_Start_Date__c = System.today().addDays(-10);
            terr1.Status__c = 'Active';
            insert terr1;
            System.assertEquals('New Territory', terr1.Name);     
            
            //Insert RREmployee__c     
            RREmployee__c emp = new RREmployee__c();
            emp.First_Name__c='Test';
            emp.Last_Name__c='Emp';
            emp.Name = 'Test emp';
            emp.Franchise__c = 'test franchise';
            emp.Territory_ID__c ='100'; 
            insert emp;
            System.assertEquals('Test emp', emp.Name);            
                        
						RREmployee_Territory__c ter = new RREmployee_Territory__c() ;
	   ter.Territory__c=terr1.Id ; 
	   ter.Active__c =true;
	   insert ter ; 
	   
	   
	   
						
            terr1.Status__c = 'Inactive';         
            terr1.Effective_End_Date__c = System.today().addDays(1);
            update terr1;
       
	   
	   
	   
	   
        }catch(DmlException e){
            System.debug('Exception Occurred: '+ e.getMessage());            
        }        
    }    

}

 
coolkrishcoolkrish
Thanks Raj for your reply.After adding that, code coverage falling from 75% to 18%.
Raj VakatiRaj Vakati
Check whether test class is running successfully or its failure  and share the lines which are not covered 
coolkrishcoolkrish
Raj - When I add the block below, I am getting List index out of bounds:0 error.Otherwise success

RREmployee_Territory__c empter = new RREmployee_Territory__c();
            empter.Employee__c = emp.Id; 
            empter.Territory__c = terr1.Id;
            empter.Start_Date__c = System.today().addDays(-5);
            empter.Active__c = True;
            insert empter; 
Raj VakatiRaj Vakati
Your are getting error from this RR_Terr_Inactive_After_Ins_Upd  trigger or any other ? If so please share the code 
coolkrishcoolkrish
Raj - I could fix the error.Now test class is success but when I put, 

           RREmployee_Territory__c empter = new RREmployee_Territory__c();
            empter.Employee__c = emp.Id; 
            empter.Territory__c = terr1.Id;

            insert empter; 
coverage is coming down to 18% with just first couple of lines covered
coolkrishcoolkrish
When I place RREmployee_Territory__c insert at the end of test class, coverage is coming to 75% with the lines in bold in the initial post still not covered.
Narender Singh(Nads)Narender Singh(Nads)
Hi 

Try this code:
@istest
public class Test_RR_Terr_Inactive_After_Ins_Upd{
	
	
	testmethod public static void testfunction(){
		
		RRTerritory__c TerritoryObject=new RRTerritory__c();
		TerritoryObject.Name ='Test Territory';
		TerritoryObject.Effective_Start_Date__c = System.today().addDays(-10);
		TerritoryObject.Status__c = 'Active';
                //If there is any other required field, assign a value to it.
		insert TerritoryObject;
		
		RREmployee_Territory__c EmployeeObject=new RREmployee_Territory__c();
		EmployeeObject.First_Name__c='Test';
        EmployeeObject.Last_Name__c='Emp';
        EmployeeObject.Name = 'Test emp';
		EmployeeObject.active__c=true;
		EmployeeObject.Territory__c=TerritoryObject.id;
                //If there is any other required field, assign a value to it.
		insert EmployeeObject;
		
		TerritoryObject.Status__c='Inactive';
		TerritoryObject.Effective_End_Date__c = System.today().addDays(1);
		update TerritoryObject;
		
	}
}

Let me know if it helps
Thanks!