You need to sign in to do that
Don't have an account?
Puneeth Kumar
Error while trying to insert opportunity line items in test class: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId]
Hello all,
I have been struggling with the above error I get when trying to insert Opportunitylineitems in the test class. Although I am passing the same values for the pricebookid and pricebookentry I am getting the above error. Could anyone please assist if they have any known solution. Thanks in advance.
Code as below:
@istest
public class TestOpportunityLineItemTriggerHandlerV2 {
static testmethod void testInsert(){
test.startTest();
Country__c c=new Country__c();
c.Name='United States';
c.Active__c=true;
insert c;
State__c s=new State__C();
s.name='Texas';
s.Country__c= c.id;
insert s;
AccountTAHelper.isAccountTriggerCall = false;
Account a = new Account();
a.name = 'test';
a.City__c='Plano';
a.Phone='235-345-4564';
a.Zip_Postal_Code__c='75025';
a.Siebel_Row_Id__c='1-AX4BC4';
a.country__c=c.id;
a.state__c=s.id;
insert a;
Contact con = new Contact();
con.firstName = 'test';
con.lastName = 'test';
con.accountId = a.Id;
insert con;
id pricebookId = Test.getStandardPricebookId();
Product2 p2 = new Product2();
p2.name = 'test';
p2.Family = 'License';
p2.IsActive =true;
insert p2;
PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = p2.Id, UnitPrice = 10000, IsActive = true);
insert standardPrice;
Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
insert customPB;
PricebookEntry customPrice = new PricebookEntry(Pricebook2Id = customPB.Id, Product2Id = p2.Id, UnitPrice = 12000, IsActive = true);
insert customPrice;
Opportunity o = new Opportunity();
o.name = 'test';
o.stageName = '07 - Closed Won';
o.accountId = a.Id;
// system.debug('Inside create oppt Pid is as:'+ pid);
o.Pricebook2id=customPB.Id;
o.closeDate = system.today().adddays(30);
o.LeadSource = 'Deal Registration';
o.Dell_Referral_Opty_ID__c='345hxrff';
insert o;
OpportunityContactRole ocr = new OpportunityContactRole();
ocr.opportunityId = o.Id;
ocr.contactId = con.Id;
insert ocr;
OpportunityLineItem oli = new OpportunityLineItem();
oli.opportunityId = o.id;
oli.PricebookentryId = customPrice.Id;
oli.TotalPrice = 500;
insert oli;
test.stopTest();
}
}
Getting error on the line - " Insert oli; "
I have been struggling with the above error I get when trying to insert Opportunitylineitems in the test class. Although I am passing the same values for the pricebookid and pricebookentry I am getting the above error. Could anyone please assist if they have any known solution. Thanks in advance.
Code as below:
@istest
public class TestOpportunityLineItemTriggerHandlerV2 {
static testmethod void testInsert(){
test.startTest();
Country__c c=new Country__c();
c.Name='United States';
c.Active__c=true;
insert c;
State__c s=new State__C();
s.name='Texas';
s.Country__c= c.id;
insert s;
AccountTAHelper.isAccountTriggerCall = false;
Account a = new Account();
a.name = 'test';
a.City__c='Plano';
a.Phone='235-345-4564';
a.Zip_Postal_Code__c='75025';
a.Siebel_Row_Id__c='1-AX4BC4';
a.country__c=c.id;
a.state__c=s.id;
insert a;
Contact con = new Contact();
con.firstName = 'test';
con.lastName = 'test';
con.accountId = a.Id;
insert con;
id pricebookId = Test.getStandardPricebookId();
Product2 p2 = new Product2();
p2.name = 'test';
p2.Family = 'License';
p2.IsActive =true;
insert p2;
PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = p2.Id, UnitPrice = 10000, IsActive = true);
insert standardPrice;
Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
insert customPB;
PricebookEntry customPrice = new PricebookEntry(Pricebook2Id = customPB.Id, Product2Id = p2.Id, UnitPrice = 12000, IsActive = true);
insert customPrice;
Opportunity o = new Opportunity();
o.name = 'test';
o.stageName = '07 - Closed Won';
o.accountId = a.Id;
// system.debug('Inside create oppt Pid is as:'+ pid);
o.Pricebook2id=customPB.Id;
o.closeDate = system.today().adddays(30);
o.LeadSource = 'Deal Registration';
o.Dell_Referral_Opty_ID__c='345hxrff';
insert o;
OpportunityContactRole ocr = new OpportunityContactRole();
ocr.opportunityId = o.Id;
ocr.contactId = con.Id;
insert ocr;
OpportunityLineItem oli = new OpportunityLineItem();
oli.opportunityId = o.id;
oli.PricebookentryId = customPrice.Id;
oli.TotalPrice = 500;
insert oli;
}
}
Getting error on the line - " Insert oli; "
the only error I have got was a REQUIRED_FIELD_MISSING on Quantity.
the above code piece fixed it.
I ran by making above changes but no luck still getting below 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]
Have you tried with standard pricebook instead of custom one? If not then try with it once and let me know if it works.
Hi LBK, unfortunately I was unable to catch the exception in debug log too as the maximum size of the debug log exceeds the limit.
I just used exception handling try-catch block while inserting the Opportunity line items I was then able to pass through the tests. But unclear what is the root cause for the error like that.