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
Scott Janis 20Scott Janis 20 

What am I doing wrong? (pricebook entry is in a different pricebook than the one assigned to the opportunity):

Hello, 

I have a test class which was created to give me coverage for a trigger. Although I know you only need 1% for a trigger to move it to production, I was able to get 79%. The problem is I keep getting the following error  

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

I am not clear where my code is rerencing 'another' price book entry. Its a cert simple test script. 


@istest
public class Populate_ExtensionTestclass{

static testmethod void  PopulateExtensiontest(){

Date closeDt = Date.Today();

 date myDate = date.today();

Account a2 = new Account(Name ='icrm testing acc');
insert a2;



    id standardPBID = Test.getStandardPricebookId(); 

 /* Pricebook2 pb22 = new Pricebook2(Name='testDIE',Effective_End_Date__c=closeDt, Effective_Start_Date__c=closeDt);
 insert pb22; */

Product2 pro2 = new Product2(Name='BXCD', isActive=true);
insert pro2;

PricebookEntry pbe2 =new PricebookEntry(unitprice=0.01,Product2Id=pro2.Id,Pricebook2Id=standardPBID,
                                         isActive=true,UseStandardPrice = false);
 insert pbe2;
 
 opportunity oppr = new opportunity(Name='testing DIE 4/6/2015' ,  AccountId= a2.Id,StageName = 'Prospecting', 
                                   CloseDate = closeDt,     Pricebook2Id = standardPBID);
 insert oppr;

 OpportunityLineItem OPplineitem2 = new OpportunityLineItem (Quantity=2, OpportunityId=oppr.Id,UnitPrice=0.01,PriceBookEntryId=pbe2.Id);
 insert OPplineitem2;
 }
 static testmethod void  PopulateExtensiontest1(){

Date closeDt = Date.Today();

 date myDate = date.today();

Account a2 = new Account(Name ='Non icrm testing acc');
insert a2;

    id standardPBID = Test.getStandardPricebookId(); 

 /* Pricebook2 pb22 = new Pricebook2(Name='testNonDIE',Effective_End_Date__c=closeDt, Effective_Start_Date__c=closeDt);
 insert pb22; */
 
opportunity oppr = new opportunity(Name='testing NONDIE 4/6/2015' ,  AccountId= a2.Id,StageName = 'Prospecting', 
                                   CloseDate = closeDt, Pricebook2Id=standardPBID);
insert oppr;

Product2 pro2 = new Product2(Name='BXCDXXX',isActive=true);
insert pro2;

PricebookEntry pbe2 =new PricebookEntry(unitprice=1,Product2Id=pro2.Id,Pricebook2Id=standardPBID,
                                         isActive=true,UseStandardPrice = false);
 insert pbe2;

 OpportunityLineItem OPplineitem2 = new OpportunityLineItem (Quantity=25, OpportunityId=oppr.Id,UnitPrice=1,PriceBookEntryId=pbe2.Id);
 insert OPplineitem2;
 }
 }
 
AnudeepAnudeep (Salesforce Developers) 
This happens if you insert a product with pricebook ABC into Product having pricebook XYZ

Example: 

Account and Opportunity have Price Level "AR5".
The customer is trying to pass a product having AR5 price book into ENDUSER price book. Since these are 2 different price books it throws an error
They have to change their logic to compare if the Product belongs to both AR5 and ENDUSER and pass ENDUSER as the pricebook entry id to insert the record.

You will have to change your logic to see if the product belongs to the correct price book and then insert the record

Opportunity.Pricebook2Id should be equal to your OLI.pricebookEntry.Pricebook2Id (See this example - https://ipfs-sec.stackexchange.cloudflare-ipfs.com/salesforce/A/question/110751.html)

Anudeep
Scott Janis 20Scott Janis 20
Does not make sense. I'm clearly referencing the same PB I used to create the Entry. Where is the reference to an alternatre PB? See the BOLD Text


@istest
public class Populate_ExtensionTestclass{

static testmethod void  PopulateExtensiontest(){

Date closeDt = Date.Today();

 date myDate = date.today();

Account a2 = new Account(Name ='icrm testing acc');
insert a2;



    

Pricebook2 pb22 = new Pricebook2(Name='testDIE',Effective_End_Date__c=closeDt, Effective_Start_Date__c=closeDt);
 insert pb22;

Product2 pro2 = new Product2(Name='BXCD', isActive=true);
insert pro2;

PricebookEntry pbe2 =new PricebookEntry(unitprice=0.01,Product2Id=pro2.Id,Pricebook2Id=pb22.ID,
                                         isActive=true,UseStandardPrice = false);
 insert pbe2;
 

 opportunity oppr = new opportunity(Name='testing DIE 4/6/2015' ,  AccountId= a2.Id,StageName = 'Prospecting', 
                                   CloseDate = closeDt,     Pricebook2Id = pb22.ID);
 insert oppr;

 OpportunityLineItem OPplineitem2 = new OpportunityLineItem (Quantity=2, OpportunityId=oppr.Id,UnitPrice=0.01,PriceBookEntryId=pbe2.Id);
 insert OPplineitem2;
 }
 static testmethod void  PopulateExtensiontest1(){

Date closeDt = Date.Today();

 date myDate = date.today();

Account a2 = new Account(Name ='Non icrm testing acc');
insert a2;

 Pricebook2 pb22 = new Pricebook2(Name='testNonDIE',Effective_End_Date__c=closeDt, Effective_Start_Date__c=closeDt);
 insert pb22;
 

Product2 pro2 = new Product2(Name='BXCDXXX',isActive=true);
insert pro2;

PricebookEntry pbe2 =new PricebookEntry(unitprice=1,Product2Id=pro2.Id,Pricebook2Id=pb22.ID,
                                         isActive=true,UseStandardPrice = false);
 insert pbe2;


opportunity oppr = new opportunity(Name='testing NONDIE 4/6/2015' ,  AccountId= a2.Id,StageName = 'Prospecting', 
                                   CloseDate = closeDt, Pricebook2Id=pb22.ID);
insert oppr;


 OpportunityLineItem OPplineitem2 = new OpportunityLineItem (Quantity=25, OpportunityId=oppr.Id,UnitPrice=1,PriceBookEntryId=pbe2.Id);
 insert OPplineitem2;
 }
 }