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
Satapon WongsiriSatapon Wongsiri 

FIELD_INTEGRITY_EXCEPTION, field integrity exception

Hi Everyone,

I stuck with this error now give me some solution.

ERROR 
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: []

Thank you for Help 

This code 
@isTest
public class UpdateOpportunityToTotalValueTest {
    @isTest
    static void testInsertOpportunityLineItems() {
       
        Pricebook2 priceBook = new Pricebook2(Name = 'Test Pricebook');
        insert priceBook;
       
        Pricebook2 standardPB = [SELECT Id FROM Pricebook2 WHERE isStandard=true LIMIT 1];
       
        Product2 product1 = new Product2(
            Name = 'Product1',
            ProductCode = 'P1',
            Description = 'Product Test 1'
        );
        Product2 product2 = new Product2(
            Name = 'Product2',
            ProductCode = 'P2',
            Description = 'Product Test 2'
        );
        insert product1;
        insert product2;
        PricebookEntry standardPrice = new PricebookEntry(
             Pricebook2Id = standardPB.Id,
             Product2Id = product1.Id,
             UnitPrice = 10000,
             IsActive = true,
             UseStandardPrice = true          
        );
        insert standardPrice;
        PricebookEntry priceCustom1 = new PricebookEntry(
            Pricebook2Id = priceBook.Id,
            Product2Id = product1.Id,
            UnitPrice = 100.00,
            UseStandardPrice = false,
            IsActive = true
        );
        insert priceCustom1;
       
        PricebookEntry priceCustom2 = new PricebookEntry(
            Pricebook2Id = priceBook.Id,
            Product2Id = product2.Id,
            UnitPrice = 50.00,
            UseStandardPrice = false,
            IsActive = true
        );
       
        insert priceCustom2;
       
        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
            StageName = 'Closed Won',
            CloseDate = Date.today(),
            TotalValue__c = 0,
            Pricebook2Id = priceBook.Id
        );
        insert testOpportunity;
       
        OpportunityLineItem oline1 = new OpportunityLineItem(
            OpportunityId = testOpportunity.Id,
            PricebookEntryId = priceCustom1.Id,
            Quantity = 2
        );
        OpportunityLineItem oline2 = new OpportunityLineItem(
            OpportunityId = testOpportunity.Id,
            PricebookEntryId = priceCustom2.Id,
            Quantity = 1
        );
        List<OpportunityLineItem> oppLineToUpdate = new List<OpportunityLineItem>{oline1, oline2};
        insert oppLineToUpdate;
        testOpportunity = [SELECT TotalValue__c FROM Opportunity WHERE Id = :testOpportunity.Id];
        System.assertEquals(250.00, testOpportunity.TotalValue__c);
    }
}
 
Julien SalensonJulien Salenson
Hi Satapon,

FIELD_INTEGRITY_EXCEPTION, indicates that there's a problem with the data you're trying to insert in Salesforce

Maybye, it could be related to the calculation of the TotalValue__c field on the Opportunity object. Is this field a formula field ? .. like that calculates the total value based on the Opportunity Line Items ? If yes, removed it.

Anyway, it's a good practice to add some debugging statements in your test code to help identify the problem. You can use System.debug() to print out variable values and debug messages.

As there are several inserts, it would be good if you could find the line number that caused the error.

Then, check the fields and values used for the object.

Please mark this comment as best answer if it's help you.
 
Arun Kumar 1141Arun Kumar 1141

Hi Satapon,

Please refer to the below link with the same discussion, it might help you with the same issue

http://salesforce.stackexchange.com/questions/142974/price-book-entry-is-in-a-different-pricebook-than-the-one-assigned-to-the-opport

Please mark it as best answer if it helps 

Thanks