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
nwingnwing 

Create QuoteLineItem In Test Class So Can test custom child Object Insertion

 

I am getting an 'Insert Failed' error that looks like this...

 

# Test Results:

Run Failures:
  BeforeUpdateQuoteLineItem_cls.BeforeUpdateOnQLI System.DmlException: 
Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING,
 Required fields are missing: [OpportunityId]: [OpportunityId]
  BeforeUpdateQuoteLineItem_trg Test coverage of selected Apex Trigger 
is 0%, at least 1% test coverage is required

 

when trying to run the below to test an insert trigger that inserts a custom child object record on the quote line item when it is updated in the appropriate way.  Obviously I never get to the update part since the insert is not functioning.  It is complaining about lacking the 'OpportunityId' but I cannot even see that field referenced on the quote line item object.

 

confused.

 

Any advice?  much appreciated.

 

 

 

public with sharing class BeforeUpdateQuoteLineItem_cls {
	public static testMethod void BeforeUpdateOnQLI()
	{
		
		Account TestRecord0 = new Account(Name='Neiltest',Type='Customer',Market__c='Phoenix');
		insert TestRecord0;
		
		Opportunity TestRecord1 = new Opportunity(Name='OppTest',Account= TestRecord0,Market__c='Phoenix',LeadSource='Internal Sales',CloseDate=Date.today()+30,StageName='Qualified');
		insert TestRecord1;
		
		Quote TestRecord2 = new Quote(Name='QuoteTest',Opportunity=TestRecord1);
		insert TestRecord2;
		
		PriceBookEntry TestRecord4 = [SELECT Id, Product2 FROM PricebookEntry WHERE Name = 'Business Complete Hosted Assurance 10Mb' limit 1];
		String ProductNewId=TestRecord4.Product2;
		
		QuoteLineItem TestRecord3 = new QuoteLineItem(Quote=TestRecord2,PriceBookEntry=TestRecord4,Bundle_Options__c=null);
		insert TestRecord3;
		TestRecord3.Bundle_Options__c = 'UPGRADE TO PREMIUM';
		
		upsert TestRecord3;

		List <Bundle_Option__c> TestRecord5 = [SELECT Id FROM Bundle_Option__c WHERE Quote_Item__c = :TestRecord3.Id];
		Integer ListCount = TestRecord5.size();
		
		System.assertEquals(1, ListCount);
	}

 

Nick34536345Nick34536345

There is an OpportunityId field on the Quote object, so I'm guessing your error is coming from the bit of code where you're inserting the Quote. I would try this:

 

Quote TestRecord2 = new Quote(Name='QuoteTest', OpportunityId=TestRecord1.Id);
insert TestRecord2;