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
Guru 91Guru 91 

Code coverage for trigger?

Hi
needed help in Code coverage

trigger UnitTrigger on Unit__c (before insert, before update) {

    if(trigger.isBefore){
    
    Map<String, Id> unitRecTypeMap = new Map<String, Id>();
    for(RecordType r: [Select Id, Name From RecordType Where sObjectType = 'Unit__c'])
        unitRecTypeMap.put(r.Name,r.Id);
    
    if(trigger.isInsert){
        for(Unit__c u: trigger.new){
            // flipping Unit to Retired if its inserted with 0 area
            if(u.Area_SF__c == 0)
            {u.Available__c = false; u.RecordTypeId = unitRecTypeMap.get('Retired'); }
            // initializing unit as unavailable if its Raw Land or Sold
            else if(u.Lifecycle__c == 'Sold')    
            u.Available__c = false;
            // initializing the unit availability to true when its first inserted with non-zero (includes null) area
            else
            u.Available__c = true;
        }
    }
    
    set<Id> unitIdSet = new set<Id>();
    
    if(trigger.isUpdate){
        for(Unit__c u: trigger.new){
            
            // updating sold units as unavailable
            if (u.Lifecycle__c == 'Sold')
                {u.Available__c = false; u.IsAvailabilityUpdatedByUser__c = false;}
            
            // Active --> Retired
            if(u.Area_SF__c == 0)
                {u.Available__c = false; u.IsAvailabilityUpdatedByUser__c = false; u.RecordTypeId = unitRecTypeMap.get('Retired'); }
            
            // Retired --> Active (very unlikely that this code will be executed)
            if(u.Area_SF__c != 0 && trigger.oldMap.get(u.Id).Area_SF__c == 0){ 
                u.RecordTypeId = unitRecTypeMap.get('Active'); 
                if(u.Lifecycle__c != 'Sold')
                    unitIdSet.add(u.Id);
            }
        }
        
        //Informatica, SSIS and other Sys Admins will skip this
            Id SysAdminId = Label.System_Administrator;
            if(LeaseUnitRelationManagement.exitUnitTrigger == false && UserInfo.getProfileId() != SysAdminId){
                for(Unit__c u: trigger.new){
                    if(u.Available__c != trigger.oldMap.get(u.Id).Available__c && u.IsAvailabilityUpdatedByUser__c == false){
                        u.IsAvailabilityUpdatedByUser__c = true;
                    }  
                }
            }
        
    }
    
        // unitIdSet - units that need to be re-evaluated for Availability (very unlikely that this code will be executed)
        if(unitIdSet.size()>0 && unitIdSet != null){ 
            UnitTriggerManagement.reEvaluateUnitsforAvailability(unitIdSet);    
        }
        
  } // end of isBefore

}
Best Answer chosen by Guru 91
Raj VakatiRaj Vakati
try this code 
 
@isTest
private class UnitTest {

static testmethod void UnitTestMethod() {        
	System.Test.startTest();
	 
	 Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
       
        
         User u = new User(
            ProfileId = profile1.Id,
            Username = 'testtermsconditions1234423@sadagshdghasd.com',
            Alias = 'batman',
            Email='testtermsconditions1234423@asdghasd.com',
            EmailEncodingKey='UTF-8',
            Firstname='Bruce',
            Lastname='Wayne',
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            TimeZoneSidKey='America/Chicago');
            insert u;
			
			        System.runas(u) {

	 Id rtypeid = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Retired').getRecordTypeId();

	 
	 Unit__c  un = new Unit__c ();
	 un.Name='Test';
	 un.Area_SF__c  =0 ;
	 un.RecordTypeId =rtypeid ;
	 insert un ;
	 
	 
	  Unit__c  un1 = new Unit__c ();
	 un1.Name='Test';
	 un1.Area_SF__c  =10 ;
	 un1.RecordTypeId =rtypeid ;
	 insert un1 ;
	 
	 un1.Lifecycle__c  ='Sold';
	 un1.Area_SF__c =0;
	 update un1 ; 
	 
					}
	
	System.Test.stopTest();
  
}

}