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
SeanSFSeanSF 

Last 3 Lines of Test Coverage Missing

 

I'm trying to get coverage for he last 3 lines of a trigger I wrote, but I can't figure out how to get these last few lines covered.  Any help is much appriciated.

 

The Test Code I have now is:

 

public class TestSetGroup4BookLU{

    static testMethod void test1(){
    
      lod4__Group__c g = new lod4__Group__c(Name='Hello'); 

      g.Group_Owner__c = UserInfo.getUserId();
     
      insert g;
      
      lod4__Event__c b = new lod4__Event__c(Name='Hello Event 2012');
      b.Group_Owner_LU__c = UserInfo.getUserId();
      b.OwnerId = UserInfo.getUserId();
     
      insert b;
 
      b = [select id, Group_Owner_LU__c from lod4__Event__c where id = :b.id];
      
      g.Sales_Executive__c = b.Group_Owner_LU__c;
      g.Group_Owner__c = UserInfo.getUserId();
    }

}

 

The test coverage that is missing is below in Red:

 

 1

 

  trigger setBookingLU4Group on lod4__Group__c (before insert, before update) {

 4   lod4__Group__c [] triggerBatch = Trigger.new;
 6   // List of Booking IDs
 7   List<String> bookIDs = new List<String>();
 9   for(lod4__Group__c g:triggerBatch){
 10   bookIDs.add(g.lod4__Event__c); // Adding BookingIDs to List
 11   }
 13   // Booking Map
 14   Map<ID, lod4__Event__c> bookMap = new Map<ID, lod4__Event__c>([select id, OwnerId, Group_Owner_LU__c from lod4__Event__c where Id = :bookIDs]);
 17   for(lod4__Group__c g:triggerBatch){
 19   if(bookMap.containsKey(g.lod4__Event__c)){
 20   lod4__Event__c book = bookMap.get(g.lod4__Event__c) ;
 21   g.Sales_Executive__c = book.OwnerId;
 22   g.Group_Owner__c = book.Group_Owner_LU__c;
 24   }
 26   }
 28

  }

Best Answer chosen by Admin (Salesforce Developers) 
Aar3538Aar3538

Hello,

 

From looking at your unit test code I see that you are inserting the  lod4__Group__c record and then inserting the lod4__Event__c record.

 

Since you have a trigger on the lod4__Group__c object your first insert initiates the trigger, however at that point there is no lod4__Event__c records, so your containsKey check (on line 19) would never have the matched value.


 

I believe what you are missing is an update g; at the end of your unit test to get the coverage you're looking for.

Also, upon closer inspection I don't see that you are setting the od4__Event__c field.  So the additional code would be:

 

g.od4__Event__c = b.Id;

update g;


Hope this helps!

 

 


 


All Answers

Aar3538Aar3538

Hello,

 

From looking at your unit test code I see that you are inserting the  lod4__Group__c record and then inserting the lod4__Event__c record.

 

Since you have a trigger on the lod4__Group__c object your first insert initiates the trigger, however at that point there is no lod4__Event__c records, so your containsKey check (on line 19) would never have the matched value.


 

I believe what you are missing is an update g; at the end of your unit test to get the coverage you're looking for.

Also, upon closer inspection I don't see that you are setting the od4__Event__c field.  So the additional code would be:

 

g.od4__Event__c = b.Id;

update g;


Hope this helps!

 

 


 


This was selected as the best answer
SeanSFSeanSF

That worked perfectly.  Thanks so much!