You need to sign in to do that
Don't have an account?
Kunlun
How can I add products into opportunitylineitem
I did a trigger to add products into opportunitylineitem
When user save opportunity, I will select some products and add them into opportunityLineItem.
But it threw an error message like below:
Insert failed. First exception on row 1; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId]: Trigger.AddProductList: line 45, column 9
What does that mean? Can't I add different pricebook into opportunityLineItem?
How can I write code to add products into opportunityLineItem?
Code:
trigger AddProductList on Opportunity (after update, after insert) { Opportunity opp = new Opportunity(); for (Opportunity oppty : System.Trigger.new) { opp = oppty; } String strProductName = 'A1_kevytmoottoripyorakotti'; Product2 product = [select Tuotelista__c from Product2 where Name = :strProductName LIMIT 1]; if(product != null) { String[] strProductList = product.Tuotelista__c.split(';', 0); List<OpportunityLineItem> oliList = new List<OpportunityLineItem>(); for(PriceBookEntry pbe : [select pbe.Id, pbe.Pricebook2Id, pbe.UnitPrice from PriceBookEntry pbe where pbe.Name in :strProductList]) { OpportunityLineItem oli = new OpportunityLineItem ( OpportunityId = opp.Id, PricebookEntryId = pbe.Id, Quantity = 1, UnitPrice = pbe.UnitPrice, ServiceDate = System.today() ); oliList.add(oli); } insert oliList; } }
When user save opportunity, I will select some products and add them into opportunityLineItem.
But it threw an error message like below:
Insert failed. First exception on row 1; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId]: Trigger.AddProductList: line 45, column 9
How can I write code to add products into opportunityLineItem?
Jeff Douglas
Informa Plc
http://blog.jeffdouglas.com
Hi,
Have you resove the issue; as I'm too facing the same problem and confused what to do now.....
Please guide me ..... I'll be very thankful to you..
waiting for reply...
Bhawna...
Hi,
Did you find solution for this? If yes please post it.
Whats wrong in my code?
I am getting error for the highlighted line:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, The pricebook entry is in a different pricebook than the one assigned to the Quote, or Quote has no pricebook assigned.: [PricebookEntryId]
Please suggest me.
Looks like you need to include the Pricebook on your quote when you create it.
Thanks Jim. It worked for me.
It should be pricebook2id=stdPb.id
For anyone else using Jeff's useful snippet of test code, you need to be aware that you cannot by default see the pricebooks from test code. You need to make sure that the test case is flagged with SeeAllData=true
Ref: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm