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
harsha B R 9harsha B R 9 

Test Class Error - Pricebook

Hello Everyone,

Facing the below error in test class.

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: unknown (pricebook entry is in a different pricebook than the one assigned to the opportunity): [unknown]

Following is the piece of code:-
 public static testmethod void updateAccumulatedPriceTest()
        Id accRecordTypeId =[SELECT id from RecordType where Name ='Partner VIP'].Id;
        //Creating Price book records
        List<Pricebook2> lstPriceBooks=new List<Pricebook2>();
        Pricebook2 objPriceBook=new Pricebook2(Name='VirtualCare',IsActive=true);
        insert lstPriceBooks;
        //Fetching system standard price book
        Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];
        //Creating an Account records
        List<account> lstAccs=new List<account>();
        Account objAcc=new Account(Name='Test Account',Envios_Automaticos__c=false,Tarifa_Acumulado__c=objPriceBook.Id,
        insert lstAccs;
        //Creating Product records
        List<Product2> lstProducts=new List<Product2>();
        Product2 objProd=new Product2(Name='Product',isActive=true);
        insert lstProducts;

        //Creating the list the opportunities
       List<Opportunity> lstOpps=new List<Opportunity>();
        Id oppRecordTypeId =[SELECT id from RecordType where Name ='Orders'].Id;
        Opportunity objOpp=new Opportunity(Name='Opportunity1',StageName='Prospecting',CloseDate=closedDate,AccountId=lstAccs[0].Id,Pricebook2Id = objPriceBook.Id,
        insert objOpp;
    //Creating PriceBookEntry Records
        List<PricebookEntry> lstPriceBookEntry=new List<PricebookEntry>();
        PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = objProd.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id=objOpp.Pricebook2Id,Product2Id = objProd.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
     insert lstPriceBookEntry;
        PriceBookEntry pbe2=[select Id, name,Pricebook2Id from Pricebookentry WHERE Pricebook2Id=:objOpp.pricebook2Id];
        //Creating Opportunity Line Item records
        List<OpportunityLineItem> lstOppItems=new List<OpportunityLineItem>();
        OpportunityLineItem objLineItem=new OpportunityLineItem(OpportunityId=objOpp.Id,Quantity=2,UnitPrice = 7500,PriceBookEntryId=pbe2.Id,Description = '2007 CMR #4 - Anti-Infectives');
           insert lstOppItems;  

The pricebookId is same and verified using debug statements.Still I am facing the above mentioned error.

Any help would be appreciated.

Thanks & Regards,
Harsha BR
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi, Harsha,

May I suggest you please refer the below link for reference.for similar kind issue. Hope it will be helpful.

Please mark it as best answer if the information is that question is removed from an unanswered question and appear as a proper solution.

Rahul Kumar
harsha B R 9harsha B R 9
Hi Rahul,

Thanks for sharing your inputs.

Provided link does not solve my issue.

I have created the opportunity and the opportunityline Item with the same pricebook but still I am facing the same issue.

Thansk & Regards,