• Jabre Celestine
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
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
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