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
Saad Ahmad 27Saad Ahmad 27 

Help with test code for apex trigger

I have the following piece of code that I am unable to figure out how to include in test. I've tried a couple of different ways but I am unable to get Line 8 included in the test scenario. Any help is greatly appreciated!! 
 
trigger SetCaseDetails on Case (before insert, before update) {
    Id ITSRecordID = O__c.getOrgDefaults().ITS_Case_Record_Type__c;
    Id TechRecordID = O__c.getOrgDefaults().TechSupportID__c;
    Map<Id, Case> parentCaseOwners = new Map<Id, Case>();
    Map<String, Repair_History__c> repairHistories = new Map<String, Repair_History__c>();
    for (Case c : Trigger.new) {
        if (c.ParentId != null &  c.Parent.Is_Owner_Active__c != false) {
            parentCaseOwners.put(c.ParentId, null);
        }
        if (c.SO__c != null) {
            repairHistories.put(c.SO__c, null);
        } if (c.Status == 'Closed' || c.Status == 'No Bid' || c.Status == 'Complete' || c.Status == 'Cancelled') {
            c.Open_Lookup__c = null;
       } else if (c.AccountId != null) {
            c.Open_Lookup__c = c.AccountId;
        }
    }    
    if (!parentCaseOwners.isEmpty()) {
        for (Case pc: [SELECT Id, OwnerId FROM Case WHERE Id in :parentCaseOwners.keySet()]) {
            parentCaseOwners.put(pc.Id,pc);
        }        
        for (Case c : Trigger.new) {
            if (c.ParentId != null & c.RecordTypeID != ITSRecordID & c.RecordTypeID != TechRecordID & c.Parent.Is_Owner_Active__c != false) 
				          
            {
                c.OwnerId = parentCaseOwners.get(c.ParentId).OwnerId;
            }
        }
    }

My test code:
 
@isTest
private class TestSetCaseDetails {
 	static testMethod void test() {    
 	Profile pf= [Select Id from profile where Name='System Administrator' limit 1]; 
        
        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,
                         isActive = True
                        ); 
      	insert uu;
      	system.debug('User active ' + uu.IsActive);
      
	System.runAs(uu){
		
        List<Repair_History__c> repairhistories = new List<Repair_History__c>();
        List<Case> caseowners = new List<Case>();
      
      	Map<id,Case> parentCaseOwners = new Map<id,Case>();
      
      	Id caserec = Schema.SObjectType.Case.getRecordTypeInfosByName().get('BA Support Case').getRecordTypeId();
      
        Account a1 = new Account(
            Name='A1',
            BillingStreet = 'Street',
            BillingCity = 'City',
            BillingPostalCode = 'Postal Code',
            BillingState = 'ON',
            BillingCountry = 'Country'        
        );
        insert a1;
        
        case c1 = new Case(
        	Status='New',
        	Origin='Email',
			SO__c='12345678',
            subject='parent case',
            Open_Lookup__c=a1.id,
            accountid=a1.id,
            ownerid=uu.Id
        	);
        insert c1;
        c1 = [select id, Is_Owner_Active__c from Case where id=:c1.id];
        system.debug('Case User Is active: ' + c1.Is_Owner_Active__c);
      	
        caseowners.add(c1);
        parentCaseOwners.put(c1.id, c1);
      
     	case c2 = new Case(
        	Status='Closed',
        	Origin='Email',
            subject='child case',
			Parentid=c1.Id,
            SO__c='12345678',
            RecordTypeId=caserec
        	);
        //insert c2;
        c2 = [select id, Is_Owner_Active__c from Case where id=:c1.id];
        system.debug('Child Case User Is active: ' + c2.Is_Owner_Active__c);
        parentCaseOwners.put(c2.id, c2);
      
      case c3 = new Case(
        	Status='Closed',
        	Origin='Email',
            subject='child case',
            Parentid=c1.Id,
            SO__c='12345678'
        	);
        insert c3;
        c3 = [select id, Is_Owner_Active__c from Case where id=:c3.id];
      	caseowners.add(c2);
        caseowners.add(c3);
        
        Account a2 = new Account(
            Name = 'A2',
            JDE_Account_Number__c='JDE',
            ParentId=a1.Id,
            BillingStreet = 'Street',
            BillingCity = 'City',
            BillingPostalCode = 'Postal Code',
            BillingState = 'ON',
            BillingCountry = 'Country'
        );
        insert a2;
        
		
		Product2 prod = new Product2(Name = 'Laptop X200', 
                                     Family = 'Hardware');
        insert prod;
        
		
		
      	Repair_History__c p1 = new Repair_History__c();
            p1.Repair_Number__c  = '123456';
            p1.Total_Price__c  = 100;
            p1.Account__c = a1.Id;
		p1.product__c = prod.Id;
            insert p1;
      
      	Repair_History__c p2 = new Repair_History__c();
            p2.Repair_Number__c  = '1234567';
            p2.Total_Price__c  = 100;
            p2.Account__c = a2.Id;
				 p2.product__c = prod.Id;
            insert p2;
      
      	repairhistories.add(p1);
        repairhistories.add(p2);
      
    }
  }
}

 
kumud thakur 20kumud thakur 20
Try to set c1.Is_Owner_Active__c=true in test class  or if this is formula field then you need to do run update on case in test class because in before insert the formula field not evaluate and it is false. 
Saad Ahmad 27Saad Ahmad 27
Thanks Kumud. It is a formula field. Not sure how would i run an update on case. I tried the following but didn't work:
 
case c1 = new Case(
        	Status='New',
        	Origin='Email',
			SO__c='12345678',
            subject='parent case',
            Open_Lookup__c=a1.id,
            accountid=a1.id,
            ownerid=uu.Id
        	);
        insert c1;     
      	c1.Status='Waiting on ITS';
        update c1;
        system.debug('Case User Is active: ' + c1.Is_Owner_Active__c);
        parentCaseOwners.put(c1.id, c1);

 
kumud thakur 20kumud thakur 20
Could you please try with this one.
case c3 = new Case(
            Status='Closed',
            Origin='Email',
            subject='child case',
            Parentid=c1.Id,
            SO__c='12345678'
            );
        insert c3;
        
        Test.startTest();
            c3 = [select id, Is_Owner_Active__c,Parent.Is_Owner_Active__c,Status,AccountId  from Case where id=:c3.id];
            update c3.SO__c='7888';
        Test.stopTest();
Raj VakatiRaj Vakati
try this
 
@isTest
private class TestSetCaseDetails {
 	static testMethod void test() {    
 	Profile pf= [Select Id from profile where Name='System Administrator' limit 1]; 
        
        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,
                         isActive = True
                        ); 
      	insert uu;
      	system.debug('User active ' + uu.IsActive);
      
	System.runAs(uu){
		
		
		O__c settings = new O__c();
    settings.ITS_Case_Record_Type__c = 'Some Value';
 settings.TechSupportID__c = 'Some Value';

    insert settings;
	
	
	
	
        List<Repair_History__c> repairhistories = new List<Repair_History__c>();
        List<Case> caseowners = new List<Case>();
      
      	Map<id,Case> parentCaseOwners = new Map<id,Case>();
      
      	Id caserec = Schema.SObjectType.Case.getRecordTypeInfosByName().get('BA Support Case').getRecordTypeId();
      
        Account a1 = new Account(
            Name='A1',
            BillingStreet = 'Street',
            BillingCity = 'City',
            BillingPostalCode = 'Postal Code',
            BillingState = 'ON',
            BillingCountry = 'Country'        
        );
        insert a1;
        
        case c1 = new Case(
        	Status='New',
        	Origin='Email',
			SO__c='12345678',
            subject='parent case',
            Open_Lookup__c=a1.id,
            accountid=a1.id,
            ownerid=uu.Id
        	);
        insert c1;
        c1 = [select id, Is_Owner_Active__c from Case where id=:c1.id];
        system.debug('Case User Is active: ' + c1.Is_Owner_Active__c);
      	
        caseowners.add(c1);
        parentCaseOwners.put(c1.id, c1);
      
     	case c2 = new Case(
        	Status='Closed',
        	Origin='Email',
            subject='child case',
			Parentid=c1.Id,
            SO__c='12345678',
            RecordTypeId=caserec
        	);
        //insert c2;
        c2 = [select id, Is_Owner_Active__c from Case where id=:c1.id];
        system.debug('Child Case User Is active: ' + c2.Is_Owner_Active__c);
        parentCaseOwners.put(c2.id, c2);
      
      case c3 = new Case(
        	Status='Closed',
        	Origin='Email',
            subject='child case',
            Parentid=c1.Id,
            SO__c='12345678'
        	);
        insert c3;
        c3 = [select id, Is_Owner_Active__c from Case where id=:c3.id];
      	caseowners.add(c2);
        caseowners.add(c3);
        
        Account a2 = new Account(
            Name = 'A2',
            JDE_Account_Number__c='JDE',
            ParentId=a1.Id,
            BillingStreet = 'Street',
            BillingCity = 'City',
            BillingPostalCode = 'Postal Code',
            BillingState = 'ON',
            BillingCountry = 'Country'
        );
        insert a2;
        
		
		Product2 prod = new Product2(Name = 'Laptop X200', 
                                     Family = 'Hardware');
        insert prod;
        
		
		
      	Repair_History__c p1 = new Repair_History__c();
            p1.Repair_Number__c  = '123456';
            p1.Total_Price__c  = 100;
            p1.Account__c = a1.Id;
		p1.product__c = prod.Id;
            insert p1;
      
      	Repair_History__c p2 = new Repair_History__c();
            p2.Repair_Number__c  = '1234567';
            p2.Total_Price__c  = 100;
            p2.Account__c = a2.Id;
				 p2.product__c = prod.Id;
            insert p2;
      
      	repairhistories.add(p1);
        repairhistories.add(p2);
      
    }
  }
}

 
Saad Ahmad 27Saad Ahmad 27
Thanks, Raj and Kumud. I tried both your suggestions but it didn't work.