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
Jabre CelestineJabre Celestine 

why is my code not covered

I wrote a trigger that fires when a new Rental (custom object) is created. What the trigger should do is query the AccountID associated with the rental, then find the Contacts associated with that accountID and associate them directly with the rental. It works in the sandbox just fine, and I was able to get it to production where it also works fine. However the code coverage is at 54% and i have no idea why.It brought my overall code coverage down to 76% and I am having a world of trouble updating this trigger to include some new code because of these coverage issues.

Also I see lots of color coding and such from different posts. what are you guys using to write your code? I am very new to this. Thank you in advance for your help.

Below is the Trigger, followed by the Test Class

TRIGGER START

//Creates the trigger AssingPrimaryBrideGroom1, whenever a new Rental record is created. the job is defined to run BEFORE the record is saved/updated
trigger AssignPrimaryContactTest on Rental__c (before insert, before update)
{
    
// Step 1: Creates a blank set to contain the AccountID supplied in a new Rental (set is named AccountIDSet on this line)
Set<String> AccountIDSet = new Set<String>();
//for each new Rental created....  
//Creates variable newRental for any new Rental(s) created
  for (Rental__c newRental : Trigger.new)
  {
    ////if the Account_Rental_c (AccountID) field is notblank
    if (newRental.Account_Rental__c != null)
    {
    //add the AccountID from the new rental to the AccountIDSet created earlier
      AccountIDSet.add(newRental.Account_Rental__c);    
    }
    
    
  }
 
  // Step 2: Query for all the contacts in Step 1
  //create a list of ALL contacts in salesforce where the position is Primary, and select only the properties needed
  List<Contact> primaryContacts = [SELECT Id, AccountId, Position__c FROM Contact WHERE AccountId IN :AccountIDSet AND Position__c = 'Primary'];
  //create a list of ALL contacts in salesforce where the Booking position is Bride, and select only the properties needed
  List<Contact> brideContacts = [SELECT Id, AccountId, Position__c, Booking_Position__c FROM Contact WHERE AccountId IN :AccountIDSet AND Booking_Position__c = 'Bride'];
  //create a list of ALL contacts in salesforce where the position is Groom, and select only the properties needed
  List<Contact> groomContacts = [SELECT Id, AccountId, Position__c, Booking_Position__c FROM Contact WHERE AccountId IN :AccountIDSet AND Booking_Position__c = 'Groom'];
 
 
    // Step 3: Make a Map that lets you search for Contacts by AccountID
  Map<String, Contact> AccountToContactMap = new Map<String, Contact>();
  //foreach Contact in the primaryContacts variable (from step 2)
  for (Contact vCnt : primaryContacts)
  {
    //put the Contact's AccountID, and the users other fields into the empty map
    AccountToContactMap.put(vCnt.AccountID, vCnt);
  }
 
      // Step 3a: Make a Map that lets you search for Contacts by AccountID
  Map<String, Contact> AccountToBrideMap = new Map<String, Contact>();
  //foreach Contact in the brideContacts variable (from step 2)
  for (Contact vBride : brideContacts)
  {
    //put the Contact's AccountID, and the users other fields into the empty map
    AccountToBrideMap.put(vBride.AccountID, vBride);
  }
 
      // Step 3b: Make a Map that lets you search for Contacts by AccountID
  Map<String, Contact> AccountToGroomMap = new Map<String, Contact>();
  //foreach Contact in the groomContacts variable (from step 2)
  for (Contact vGroom : groomContacts)
  {
    //put the Contact's AccountID, and the users other fields into the empty map
    AccountToGroomMap.put(vGroom.AccountID, vGroom);
  }
 
   // Step 4: Get the matching Contact in the Map by AccountID!
  //for each new case created
  for (Rental__c newRental : Trigger.new)
  {
    if (newRental.Rental_Event_Type__c == 'Wedding/ Reception')
    {   
        if (newRental.Account_Rental__c != null)
        {
            //reference the map, retrieving the AccountID matching what was supplied in the rental record
          Contact aContact = AccountToContactMap.get(newRental.Account_Rental__c);
          if (aContact != null)
          {
            // the new case Primary_Rental_Contact__c will  be set to the ID retrieved from the map
            newRental.Primary_Rental_Contact__c = aContact.Id;
          }
          
          Contact bContact = AccountToBrideMap.get(newRental.Account_Rental__c);
          if (bContact != null)
          {
            // the new case Primary_Rental_Contact__c will  be set to the ID retrieved from the map
            newRental.Bride__c = bContact.Id;
          }
          
          Contact gContact = AccountToGroomMap.get(newRental.Account_Rental__c);
          if (gContact != null)
          {
            // the new case Primary_Rental_Contact__c will  be set to the ID retrieved from the map
            newRental.Groom__c = gContact.Id;
          }
        }
        

    }
    else
    {
        //if the new rentals AccountID is not null
        if (newRental.Account_Rental__c != null)
        {
          //reference the map, retrieving the AccountID matching what was supplied in the rental record
          Contact aContact = AccountToContactMap.get(newRental.Account_Rental__c);
          if (aContact != null)
          {
            // the new case Primary_Rental_Contact__c will  be set to the ID retrieved from the map
            newRental.Primary_Rental_Contact__c = aContact.Id;
          }
        }       
    
    }
  }
 
 }

TRIGGER END

CLASS START


@isTest
public class TestAssignPrimaryProd1 {
    static testMethod void insertNewRental() {
       
       Rental__C rentalToCreate = new Rental__C();
       
       // Do you recognize these fields?
       rentalToCreate.RecordTypeID  = '0126A000000Do2IQAS';
       rentalToCreate.Account_Rental__c = '0016A000009A6pUQAS';
       rentalToCreate.Rental_Date__c  = date.newInstance(2014, 9, 15);
       rentalToCreate.Name  = 'new';
       rentalToCreate.Rental_Event_Type__c  = 'Private_Event';

       
       insert rentalToCreate;
    }
}


CLASS END
Jabre CelestineJabre Celestine
Sorry for the double post, i am also new to the forums... Here is a view of the dev console that shows the code that is not working.

When i am testing the code, i am using an account that has contacts that fit all three criteria. A Bride, a Groom, and a Primary. Like i said, it does work in production. I just need to get the coverage higher.


User-added imageUser-added image
Jabre CelestineJabre Celestine
Figured it out guys. The issue was that i had an ELSE statement that was not being tested. The else statement contained a condition that was impossible to meet which basically said if a rental is created without an associated account then do this.... but account association is required for the rental to be created. Simply removing that part of the code brought my percentage up and i am able to deploy. here is the new code below.


//Creates the trigger AssingPrimaryBrideGroom1, whenever a new Rental record is created. the job is defined to run BEFORE the record is saved/updated
trigger AssignPrimaryContactTest on Rental__c (before insert, before update) 
{
    
// Step 1: Creates a blank set to contain the AccountID supplied in a new Rental (set is named AccountIDSet on this line)
Set<String> AccountIDSet = new Set<String>();
//for each new Rental created....  
//Creates variable newRental for any new Rental(s) created
  for (Rental__c newRental : Trigger.new) 
  {
    ////if the Account_Rental_c (AccountID) field is notblank
    if (newRental.Account_Rental__c != null) 
    {
    //add the AccountID from the new rental to the AccountIDSet created earlier
      AccountIDSet.add(newRental.Account_Rental__c);    
    }
    
    
  }
  
  // Step 2: Query for all the contacts in Step 1
  //create a list of ALL contacts in salesforce where the position is Primary, and select only the properties needed (list is name primaryContacts)
  List<Contact> primaryContacts = [SELECT Id, AccountId, Position__c FROM Contact WHERE AccountId IN :AccountIDSet AND Position__c = 'Primary'];
  //create a list of ALL contacts in salesforce where the position is Primary, and select only the properties needed (list is name primaryContacts)
  List<Contact> brideContacts = [SELECT Id, AccountId, Position__c, Booking_Position__c FROM Contact WHERE AccountId IN :AccountIDSet AND Booking_Position__c = 'Bride'];
  //create a list of ALL contacts in salesforce where the position is Primary, and select only the properties needed (list is name primaryContacts)
  List<Contact> groomContacts = [SELECT Id, AccountId, Position__c, Booking_Position__c FROM Contact WHERE AccountId IN :AccountIDSet AND Booking_Position__c = 'Groom'];
  //create a list of ALL contacts in salesforce where the position is Primary, and select only the properties needed (list is name primaryContacts)
  List<Contact> bride1Contacts = [SELECT Id, AccountId, Position__c, Booking_Position__c FROM Contact WHERE AccountId IN :AccountIDSet AND Booking_Position__c = 'Bride 1'];
  //create a list of ALL contacts in salesforce where the position is Primary, and select only the properties needed (list is name primaryContacts)
  List<Contact> groom1Contacts = [SELECT Id, AccountId, Position__c, Booking_Position__c FROM Contact WHERE AccountId IN :AccountIDSet AND Booking_Position__c = 'Groom 1'];
  
  
    // Step 3: Make a Map that lets you search for Contacts by AccountID (map name is AccountToContactMap)
    
    //PRIMARY CONTACT
  Map<String, Contact> AccountToContactMap = new Map<String, Contact>();
  //foreach Contact in the primaryContacts variable (from step 2)
  for (Contact vCnt : primaryContacts) 
  {
    //put the Contact's AccountID, and the users other fields into the empty map
    AccountToContactMap.put(vCnt.AccountID, vCnt);
  }
  
  //BRIDE
  Map<String, Contact> AccountToBrideMap = new Map<String, Contact>();
  //foreach Contact in the brideContacts variable (from step 2)
  for (Contact vBride : brideContacts) 
  {
    //put the Contact's AccountID, and the users other fields into the empty map
    AccountToBrideMap.put(vBride.AccountID, vBride);
  }
  
  //GROOM
  Map<String, Contact> AccountToGroomMap = new Map<String, Contact>();
  //foreach Contact in the groomContacts variable (from step 2)
  for (Contact vGroom : groomContacts) 
  {
    //put the Contact's AccountID, and the users other fields into the empty map
    AccountToGroomMap.put(vGroom.AccountID, vGroom);
  }
  
  //BRIDE 1
  Map<String, Contact> AccountToBride1Map = new Map<String, Contact>();
  //foreach Contact in the brideContacts variable (from step 2)
  for (Contact vBride1 : bride1Contacts) 
  {
    //put the Contact's AccountID, and the users other fields into the empty map
    AccountToBride1Map.put(vBride1.AccountID, vBride1);
  }
  
  //GROOM 1
  Map<String, Contact> AccountToGroom1Map = new Map<String, Contact>();
  //foreach Contact in the groomContacts variable (from step 2)
  for (Contact vGroom1 : groom1Contacts) 
  {
    //put the Contact's AccountID, and the users other fields into the empty map
    AccountToGroom1Map.put(vGroom1.AccountID, vGroom1);
  }  

  
   // Step 4: Get the matching Contact in the Map by AccountID!
  //for each new case created
  for (Rental__c newRental : Trigger.new) 
  {
    if (newRental.Rental_Event_Type__c == 'Wedding/ Reception')
    {   
        if (newRental.Account_Rental__c != null)
        {
            //reference the map, retrieving the AccountID matching what was supplied in the rental record
          Contact aContact = AccountToContactMap.get(newRental.Account_Rental__c);
          if (aContact != null) 
          {
            //PRIMARY CONTACT
            newRental.Primary_Rental_Contact__c = aContact.Id;
          }
          
          Contact bContact = AccountToBrideMap.get(newRental.Account_Rental__c);
          if (bContact != null) 
          {
            //BRIDE
            newRental.Bride__c = bContact.Id;
          }
          
          Contact gContact = AccountToGroomMap.get(newRental.Account_Rental__c);
          if (gContact != null) 
          {
            //GROOM
            newRental.Groom__c = gContact.Id;
          }
          
          Contact b1Contact = AccountToBride1Map.get(newRental.Account_Rental__c);
          if (b1Contact != null) 
          {
            //BRIDE1
            newRental.Bride_1__c = b1Contact.Id;
          }
          
          Contact g1Contact = AccountToGroom1Map.get(newRental.Account_Rental__c);
          if (g1Contact != null) 
          {
            //GROOM1
            newRental.Groom_1__c = g1Contact.Id;
          }
          
        }
        

    }
    else
    {
    if (newRental.Account_Rental__c != null)
        {
            //reference the map, retrieving the AccountID matching what was supplied in the rental record
          Contact aContact = AccountToContactMap.get(newRental.Account_Rental__c);
          if (aContact != null) 
          {
            //PRIMARY CONTACT
            newRental.Primary_Rental_Contact__c = aContact.Id;
          }
        
    }

  }
  
 }
}
Andray GaustAndray Gaust
Can i insert that code in website like Marrakech wedding film (https://www.wedding-focus.com/marrakech-wedding-film/) where people submit there detail and future date so we can predict the ads to show to user.