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()
    {
        Date closedDate=System.today()+1;
        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);
        lstPriceBooks.add(objPriceBook);
        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,
                                   Contact_email__c='dummy@email11.com',RecordTypeId=accRecordTypeId);
        lstAccs.add(objAcc);
        insert lstAccs;
        
        
        //Creating Product records
        List<Product2> lstProducts=new List<Product2>();
        Product2 objProd=new Product2(Name='Product',isActive=true);
        lstProducts.add(objProd);
        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,
                                          RecordTypeId=oppRecordTypeId,Viene_de__c=lstAccs[0].Id,Email_de_Contacto__c='dummyOpp@email.com',
                                          Codigo_Postal_de_Envio__c='tesage',Calle_de_Envio__c='tesst',Contacto_Destinatario_Mens__c='cpntacto',
                                          Ciudad_de_Envio__c='ciudad',Pais_de_Envio__c='Pais',Provincia_de_Envio__c='provincia',Telefono__c='1244556677',
                                          Email_Destinatario_Mens__c='dummyOpp1@email.com');
               
        insert objOpp;
    system.debug('***objOpp***'+objOpp.Pricebook2Id);
    //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);
        lstPriceBookEntry.add(standardPrice);
        
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id=objOpp.Pricebook2Id,Product2Id = objProd.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        lstPriceBookEntry.add(pbe); 
     insert lstPriceBookEntry;
    
        PriceBookEntry pbe2=[select Id, name,Pricebook2Id from Pricebookentry WHERE Pricebook2Id=:objOpp.pricebook2Id];
        system.debug('***pbe2.Id***'+pbe2.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');
        lstOppItems.add(objLineItem);
        
        if(!lstOppItems.isEmpty())
        {
           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 informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
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,
Harsha