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
Ronaldo CostaRonaldo Costa 

Test class - trigger to create child records

Hello,

Please review the code below, I don't get why its not passing 100%

Trigger:
 
trigger CreateFlightItinerary on Opportunity (after update) {    
List<Itinerary__c> fi = new List<Itinerary__c>();
    for (Opportunity a: Trigger.New)
         if (a.fi_automation_check__c == TRUE){
                 <b>fi.add (new Itinerary__c(</b>
                     Flight_Booking__c = a.Id,
					 Stop_1__c = a.Stop1_ID__c,
					 Flight_Crew_1__c = a.CrewM_1_ID__c
				));
         }
   insert fi;
}

Test class:
 
@isTest
private class testCreateFlightItinerary{
    static TestMethod void myTestClass()
    {
    	Account a = new Account();
	    a.Name = 'TestName';
	    a.Phone = '5512991224391';
        insert a;
    	
    	Opportunity o = new Opportunity();
 		o.Name = 'TestName';
		o.AccountID = a.Id;
		o.closedate = system.today();
		o.stagename = 'Sample Quote';
		o.Aircraft_Type__c = 'King Air';
		o.fi_automation_check__c=false;
		insert o; 
		
		o.fi_automation_check__c=true;
		update o;
    }
}

Thanks!
David ZhuDavid Zhu

You have to set o.fi_automation_check__c to true in test class. line 16
And after insert opportunity, you need to check if itinerary__c record is inserted (cover line 5  to 8 in trigger).
You can use the following code to cover 100% code.
 
BTW, I would suggest change the trigger at line 11:

if (fi.size() > 0)
insert fi;
@isTest
private class testCreateFlightItinerary{
    static TestMethod void myTestClass()
    {
    	Account a = new Account();
	    a.Name = 'TestName';
	    a.Phone = '5512991224391';
        insert a;
    	
    	Opportunity o = new Opportunity();
 		o.Name = 'TestName';
		o.AccountID = a.Id;
		o.closedate = system.today();
		o.stagename = 'Sample Quote';
		o.Aircraft_Type__c = 'King Air';
		o.fi_automation_check__c=true;
	insert o; 

	
	system.assertequals(1,[select count() from itinerary__c where Flight_Booking__c = :o.Id]);
		
    }
}

 
Arun KharbArun Kharb
You cannot define your own stage into standard object's picklist field. 
Try After changing  
o.stagename = 'Sample Quote';

to 
o.StageName= 'Prospecting';
Hope this will fix your issue. 
Ronaldo CostaRonaldo Costa
Hi guys, thanks for the reply.


However it still at 80%, any thoughts? Same line uncovered...


Thanks very much.
David ZhuDavid Zhu
Wierd. is line 16:

o.fi_automation_check__c=true;

 
Ronaldo CostaRonaldo Costa
Yeah David, this is so weird.

Any ideas? How can I make sure it created the Itinerary record? I will modify the code one more time and try it again.
Ronaldo CostaRonaldo Costa
Still at 80%...
 
@isTest
private class testCreateFlightItinerary{
    static TestMethod void myTestClass()
    {
    	Account a = new Account();
	    a.Name = 'TestName';
	    a.Phone = '5512991224391';
        insert a;

		Flight_Crew__c fc1 = new Flight_Crew__c();
	    fc1.Name = 'TestFCName';
        insert fc1;
        
    	Airport__c a1 = new Airport__c();
	    a1.Name = 'TestName';
        a1.Locale__Latitude__s = 32.1356;
        a1.Locale__Longitude__s = -81.21;
        insert a1;        
 
     	Stop__c st1 = new Stop__c();
	    st1.Airport__c = a1.Id;
        insert st1;

    	Opportunity o = new Opportunity();
 		o.Name = 'TestName';
		o.AccountID = a.Id;
		o.closedate = system.today();
		o.stagename = 'Prospecting';
		o.Aircraft_Type__c = 'King Air';
		o.fi_automation_check__c=false;
		insert o; 
		
		fc1.Opportunity__c = o.Id;
		st1.Flight_Booking__c = o.Id;
		
		update fc1;
		update st1;
		
		o.fi_automation_check__c=true;
		o.Stop1_ID__c = st1.Id;
		o.CrewM_1_ID__c = fc1.Id;
		update o;
		
		system.assertequals(1,[select count() from itinerary__c where Flight_Booking__c = :o.Id]);
		
    }
}

 
David ZhuDavid Zhu
check line 30, you still set false.
You don't need code from line 33 to line 42.
Ronaldo CostaRonaldo Costa
I changed to true, still the same line

fi.add (new Itinerary__c(

not covered...

Any additional ideas??