You need to sign in to do that
Don't have an account?
harsha 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
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
May I suggest you please refer the below link for reference.for similar kind issue.
- https://developer.salesforce.com/forums/?id=906F000000090d7IAA
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
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