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
MarkInAtlantaMarkInAtlanta 

PricebookEntry throwing FIELD_INTEGRITY_EXCEPTION in test class

So i have a need to develop a test class, and it requires i create some opportunities and lines. I have others where we use SeeAllData, but since this was a new process i wanted to try and get around that.. 

on the pricebookentry i am getting error  - 
15:23:46:000 FATAL_ERROR Class.opp_test.ExpectToTest: line 21, column 1  (which points to insert of PricebookEntry
15:23:46:113 FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error:  FIELD_INTEGRITY_EXCEPTION, field integrity exception: []
Thats it for the FIELD_INTEGRITY_EXCEPTION - no other hints.. 


Here is a simple test that throws the error 

@isTest 
public class opp_test {
    
static testMethod void ExpectToTest() { 
    
        //Create test product: 
        Product2 testProductDispenser = new Product2(); 
        testProductDispenser.name = 'testDispenser'; 
        testProductDispenser.ProductCode = 'adisp'   ;  
        testProductDispenser.IsActive = true; 
        insert testProductDispenser; 
 
        Id thepricebookId = Test.getStandardPricebookId(); 
    
        PricebookEntry PEDispenser = new PricebookEntry(); 
    
        PEDispenser.Pricebook2Id = thepricebookId; 
        PEDispenser.Product2Id = testProductDispenser.Id; 
        PEDispenser.UseStandardPrice = TRUE; 
        PEDispenser.UnitPrice = 0.00; 
        PEDispenser.IsActive = TRUE; 
        insert PEDispenser;  
}   
}

there are ID's in the pricebook and product and existing entries look just like what I have in code? 
Im going to drop bact to the old merthod if i cant get this figured out (seealldata) 
Best Answer chosen by MarkInAtlanta
MarkInAtlantaMarkInAtlanta
well..  seems my threats worked!  took a hard look at the old test code that used SeeAllData and it  turns out this was self inflicted - seems there was a method in the Product Trigger that automagicly set the PriceBookEntry...    which of course was rolled back when test class ended ...  Arghhhh..  

still not sure of the FIELD_INTEGRITY_EXCEPTION but it is working now..