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
Muna Jamous5Muna Jamous5 

I am a newbie, I need to write a test class for the trigger below

trigger updateChangeDetails on Change_Request__c (before insert, before update) {
  try {
          
     For (Change_Request__c r: Trigger.New) {     
         if (r.RecordTypeId =='0126A000000IHN9QAO' && r.Change_Details__c == null ) {      
            Equipment_in_Room__c[] roomID  = [Select id , Plan_Id__c, Room__c,Hospital_Room__c from Equipment_in_Room__c WHERE id =: r.Equipment_Room_ID__c limit 1] ;             
            Hospital_Room_No__c[] hospitalRoom  = [Select id , Department__c,     Room_Name__c, Room_No__c from Hospital_Room_No__c WHERE id =: roomID[0].Hospital_Room__c limit 1] ;
            Equipment_Specification__c[] equipmentDetails  = [Select id , name from Equipment_Specification__c WHERE id =: roomID[0].Plan_Id__c limit 1] ; 
             if ( roomID.size() > 0 ) {
                r.Change_Details__c = 
                   'Room # : '+ hospitalRoom[0].Room_No__c + '\\n  ' +
                   'Room Name : ' + hospitalRoom[0].Room_Name__c + '\\n  ' +
                   'Plan ID : ' + equipmentDetails[0].name + '\\n  ' +
                   'Department : ' + hospitalRoom[0].Department__c; 
             } 
         } else if (r.RecordTypeId == '0126A000000IHNEQA4'&& r.Change_Details__c == null ) {  //Furniture in a room Change
              Furniture_in_a_Room__c[] roomID  = [Select id,Plan_ID__c, Hospital_Room__c from Furniture_in_a_Room__c WHERE id =:r.Furniture_Room_ID__c limit 1] ;             
              Hospital_Room_No__c[] hospitalRoom  = [Select id , Department__c,     Room_Name__c, Room_No__c from Hospital_Room_No__c WHERE id =: roomID[0].Hospital_Room__c limit 1] ;
              Furniture_Specification__c[] furnitureDetails  = [Select id , name from Furniture_Specification__c WHERE id =: roomID[0].Plan_Id__c limit 1] ; 
             if ( roomID.size() > 0 ) {
                r.Change_Details__c = 
                   'Room # : '+ hospitalRoom[0].Room_No__c + '\\n  ' +
                   'Room Name : ' + hospitalRoom[0].Room_Name__c + '\\n  ' +
                   'Plan ID : ' + furnitureDetails[0].name + '\\n  ' +
                   'Department : ' + hospitalRoom[0].Department__c; 
             }
        }    
      } //for
  }catch(Exception e){
        System.Trigger.new[0].addError('Error in updateChangeDetails: '+e.getMessage());
  }

}
Raj VakatiRaj Vakati
Sample code
 
@isTest
private class  updateChangeDetailsTest{
	
	 static testMethod void testEx() {
       RecordType  rtypes = [Select Name, Id From RecordType 
                  where Id='0126A000000IHN9QAO' and isActive=true];

				
Change_Request__c cr = new Change_Request__c();
cr.Name ='Test';
cr.RecordTypeId = rtypes.id ; 

	insert cr ; 
	
	Hospital_Room_No__c ho = new Hospital_Room_No__c() ;
	ho.Name ='Tets';
	insert ho ;

Equipment_in_Room__c er = new Equipment_in_Room__c();
er.Department__c ='Demo' ;
insert er ;


Equipment_Specification__c es = new Equipment_Specification__c() ;
es.Name='Test' ;
insert es ;
	
	  
    }
	
	
	 static testMethod void testEx1() {
       RecordType  rtypes = [Select Name, Id From RecordType 
                  where Id='0126A000000IHNEQA4' and isActive=true];

				
Change_Request__c cr = new Change_Request__c();
cr.Name ='Test';
cr.RecordTypeId = rtypes.id ; 

	insert cr ; 
	
	Hospital_Room_No__c ho = new Hospital_Room_No__c() ;
	ho.Name ='Tets';
	insert ho ;

Equipment_in_Room__c er = new Equipment_in_Room__c();
er.Department__c ='Demo' ;
insert er ;


Equipment_Specification__c es = new Equipment_Specification__c() ;
es.Name='Test' ;
insert es ;
	
	  
    }
	
}

 
Muna Jamous5Muna Jamous5
Hi Rajamohan, I used your sample code as a guidance to write the following test class, however, the coverage is still 30%, the if statement part is not being covered, I’m not sure how to cover it, any help is highly appreciated @isTest public class toUpdateChangeDetailsTest { static testMethod void toUpdateChangeDetailsTest1() { RecordType rtypes; rtypes = [Select Name, Id From RecordType where Id='0126A000000HwTf' and isActive=true]; Hospital_Room_No__c hr = new Hospital_Room_No__c() ; hr.id =rtypes.id; hr.Room_No__c = '123Test'; hr.Department__c = 'ATRIUM'; hr.Room_Name__c = 'REATIL SPACE'; hr.Floor_No__c = 'LEVEL 1'; insert hr ; Equipment_Specification__c es = new Equipment_Specification__c(); es.name ='Test123' ; es.Description__c = 'Test desc'; es.Price__c = 123.12; insert es ; Furniture_Specification__c fd = new Furniture_Specification__c(); fd.name ='Test123' ; fd.Furniture_Description__c = 'Test desc'; fd.Price__c = 123.12; fd.Installation_Cost__c = 11.12; fd.Model__c ='test'; insert fd ; Equipment_in_Room__c er = new Equipment_in_Room__c(); er.Hospital_Room__c = hr.Id ; er.Plan_Id__c = es.id; er.Qty__c = 1; er.Room_Qty__c =1; insert er ; Furniture_in_a_Room__c fr = new Furniture_in_a_Room__c (); fr.Plan_ID__c = fd.id; fr.Hospital_Room__c = hr.id; fr.Qty__c = 1; insert fr; rtypes = [Select Name, Id From RecordType where Id='0126A000000IHN9QAO' and isActive=true]; // changes in Equipment in a Room Change_Request__c cre = new Change_Request__c(); cre.RecordTypeId = rtypes.id ; cre.Change_Request_Date__c = date.today(); cre.Change_Request_Impl_Date__c = date.today(); cre.Equipment_Room_ID__c = er.Id; cre.Requester__c='IHP'; insert cre ; rtypes = [Select Name, Id From RecordType where Id='0126A000000IHNEQA4' and isActive=true]; // changes in Equipment in a Room Change_Request__c crf = new Change_Request__c(); crf.RecordTypeId = rtypes.id ; crf.Change_Request_Date__c = date.today(); crf.Change_Request_Impl_Date__c = date.today(); crf.Equipment_Room_ID__c = fr.Id; crf.Requester__c='IHP'; insert crf ; } }
Muna Jamous5Muna Jamous5
Hi Rajamohan,
I used your sample code as a guidance to write the following test class, however, the coverage is still 30%, the if statement part is not being covered, I’m not sure how to cover it, any help is highly appreciated
@isTest
public class toUpdateChangeDetailsTest {
                static testMethod void toUpdateChangeDetailsTest1() {
       
        RecordType  rtypes;
       
        rtypes = [Select Name, Id From RecordType where Id='0126A000000HwTf' and isActive=true];
        Hospital_Room_No__c hr = new Hospital_Room_No__c() ;
        hr.id =rtypes.id;
        hr.Room_No__c = '123Test';
                                hr.Department__c = 'ATRIUM';
        hr.Room_Name__c = 'REATIL SPACE';
        hr.Floor_No__c = 'LEVEL 1';
        insert hr ;
                               
        Equipment_Specification__c es = new Equipment_Specification__c();
        es.name ='Test123' ;
        es.Description__c = 'Test desc';
        es.Price__c = 123.12;       
        insert es ;
        
        Furniture_Specification__c fd = new Furniture_Specification__c();
        fd.name ='Test123' ;
        fd.Furniture_Description__c = 'Test desc';
        fd.Price__c = 123.12;   
        fd.Installation_Cost__c = 11.12;
        fd.Model__c ='test';
        insert fd ;
        
        Equipment_in_Room__c er = new Equipment_in_Room__c();
        er.Hospital_Room__c = hr.Id  ;
        er.Plan_Id__c = es.id;
        er.Qty__c = 1;
        er.Room_Qty__c =1;
        insert er ;
 
                Furniture_in_a_Room__c fr = new Furniture_in_a_Room__c ();
        fr.Plan_ID__c = fd.id;
        fr.Hospital_Room__c = hr.id;
        fr.Qty__c = 1; 
        insert fr;
       
        rtypes = [Select Name, Id From RecordType where Id='0126A000000IHN9QAO' and isActive=true];
        // changes in Equipment in a Room
        Change_Request__c cre = new Change_Request__c();
        cre.RecordTypeId = rtypes.id ;
        cre.Change_Request_Date__c = date.today();
        cre.Change_Request_Impl_Date__c = date.today();
        cre.Equipment_Room_ID__c = er.Id;
        cre.Requester__c='IHP';
                                insert cre ;
       
        rtypes = [Select Name, Id From RecordType where Id='0126A000000IHNEQA4' and isActive=true];
        // changes in Equipment in a Room
        Change_Request__c crf = new Change_Request__c();
        crf.RecordTypeId = rtypes.id ;
        crf.Change_Request_Date__c = date.today();
        crf.Change_Request_Impl_Date__c = date.today();
        crf.Equipment_Room_ID__c = fr.Id;
        crf.Requester__c='IHP';
                                insert crf ;
       
        
     }
}