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
Josh VJosh V 

Test Class for simple Trigger not working (before insert Quote)

Hey All,

 

I spent around 20 minutes making a trigger that runs flawlessly on my sandbox, and 9 hours trying to create a test class for it.  This is my first trigger and I need a little help moving past one point.

 

Here's the functional trigger:

 

trigger trgOppQuote on Quote (before insert) {

  // Pull the Parent Opportunity ID
  ID ParentOppRecordId = Trigger.new[0].OpptyID__c;
 	
  // Use the pulled Opportunity ID to select Tier, Model and HVAC fields from Parent Opportunity
    list<Opportunity> ParentRecord = 
    [SELECT Tier__c, Model_Name__c, HVAC__c FROM Opportunity WHERE id = :ParentOppRecordId ];
		
  String OppTier =  ParentRecord[0].Tier__c;
  String OppModel = ParentRecord[0].Model_Name__c;
  String OppHVAC =  ParentRecord[0].HVAC__c;
		
  // Checks if the user supplied form info, if they didn't the info is pulled from the Opportunity
  if(Trigger.new[0].Tier__c == null) 	Trigger.new[0].Tier__c = 	OppTier;
  if(Trigger.new[0].Model__c == null) Trigger.new[0].Model__c = 	OppModel;
  if(Trigger.new[0].HVAC__c == null) 	Trigger.new[0].HVAC__c = 	OppHVAC;
}

 and the faulty test class:

 

@isTest
private class TesttrgOppQuote {
	
  static testMethod void ValidQuoteInsert() {
		
    List<Opportunity> thisOpp = 
    [SELECT id, Name FROM Opportunity WHERE id = '006V0000002AQH0' ];
		
	Quote quote = new Quote(
	  Opportunity = thisOpp,
	  Delivery_Fee__c = 0.00,
	  Name = 'Test',
	  Turn_Key__c = 'Owner Managed',
	  Concrete_Pump__c = 600,
	  Foundation_Extra__c = 2000
	);
				
	insert quote;
  }
}

 

The test gets stuck at the "insert quote;" line, however it's not giving me any errors that I understand well enough to fix...  It says:

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, trgOppQuote: execution of BeforeInsert

 

Does anyone have any ideas that could nudge me in the correct direction?  I've tried numerous different methods, none of which seem to get me past the insert.

Best Answer chosen by Admin (Salesforce Developers) 
Josh VJosh V

I figured out the solution, and I feel like running screaming for joy after how much time this took...!

 

in addition to referencing the opportunity query results, I also needed OpportunityId.  So I added a line which gathered the id and placed it into my quote insert. :D

 

Here is the final Test Class with 100% coverage:

 

@isTest
private class TesttrgOppQuote {
  
  static testMethod void ValidQuoteInsert() {

  List<Opportunity> thisOpp = 
  [SELECT id, Name, Site_State__c FROM Opportunity WHERE id = '006V0000002AQH0' ];
		
  ID OppID = thisOpp[0].id;
		
  Quote quote = new Quote(
    Opportunity = thisOpp,
    OpportunityId = OppID,
    Delivery_Fee__c = 0.00,
    Name = 'Test',
    Turn_Key__c = 'Owner Managed',
    Concrete_Pump__c = 600,
    Foundation_Extra__c = 2000
  );
		
  insert quote;
  }
}