You need to sign in to do that
Don't have an account?

Help with test class for inserting OppLineItem
HI,
Need help with insering the Oppline Item. There are lot of discussions in this board about this and i even followed them but still couldnt insert the Opportunity Line Item The error i get is
FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId]
Pricebook2 pb= [select Id from PriceBook2 where IsStandard=True limit 1];
Product2 product = new Product2();
product.Name='Test Product';
product.IsActive=true;
insert product ;
// Create a pricebook entry for custom pricebook
PricebookEntry pbe = new PricebookEntry();
pbe.UseStandardPrice = false;
pbe.Pricebook2Id=pb.id;
pbe.Product2Id=product.id;
pbe.IsActive=true;
pbe.UnitPrice=99;
insert pbe;
Opportunity objOpp1= new Opportunity();
objOpp1.Name = 'Testing12';
objOpp1.StageName = 'Upside';
objOpp1.CloseDate = System.Today();
insert objOpp1;
OpportunityLineItem NewRec = new OpportunityLineItem();
NewRec.TotalPrice=99;
NewRec.Quantity=1;
NewRec.OpportunityId=objOpp1.Id;
NewRec.PricebookEntryId=pbe.id;
insert NewRec;
Thanks
An easy fix for you would be to explicitly set the Pricebook on your Opportunity before you insert it (objOpp1.Pricebook2Id = pb.Id).
Nope, I tried that already , it didnt work :(
Have you tried querying your Opportunity after you insert it to confirm the Pricebook2Id is what you expect?
Please see the following article. It may be useful
http://forums.sforce.com/t5/Apex-Code-Development/unit-test-error-standard-price-not-defined/td-p/263455#M45933
I tried to query on the Opportunity and found out that tha pricebook id's are different
and i also tried it the way it is mentioned in this post http://forums.sforce.com/t5/Apex-Code-Development/unit-test-error-standard-price-not-defined/td-p/26...
still no luck :(
Can you provide your updated code? Do you have any triggers or any other functionality that would change the pricebook of an Opportunity after insert?
This is my upaetd code and there are triggers on OpportunityLineItems but i think they dont effect in inserting a opportunityLineitem
Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true);
insert pb;
Product2 product = new Product2();
product.Name='Test Product';
product.IsActive=true;
insert product ;
// Create a pricebook entry for custom pricebook
PricebookEntry pbe = new PricebookEntry();
pbe.UseStandardPrice = false;
pbe.Pricebook2Id=pb.id;
pbe.Product2Id=product.id;
pbe.IsActive=true;
pbe.UnitPrice=99;
insert pbe;
Opportunity objOpp1= new Opportunity();
objOpp1.Name = 'Testing12';
objOpp1.StageName = 'Upside';
objOpp1.CloseDate = System.Today();
insert objOpp1;
OpportunityLineItem NewRec = new OpportunityLineItem();
NewRec.TotalPrice=99;
NewRec.Quantity=1;
NewRec.OpportunityId=objOpp1.Id;
NewRec.PricebookEntryId=pbe.id;
insert NewRec;
You still aren't explicitly setting Pricebook on your Opportunity before you insert it (objOpp1.Pricebook2Id = pb.Id).
I did that. it did not work.
Now iam getting a different error
System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []
You should go back to using the standard price book, because you created a new non-standard pricebook. If you want to use a non-standard pricebook, you first have to put a price for that Product in the standard pricebook.
I tried that too
here is my code. still its giving the error FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId].
I faced the same problem. You can use the following example.
But in my case I used the (SeeAllData=true) annotation to get access to the standard Pricebook.
Also I created the List of Opportunity type and OpportunityLineItem type to check the ScriptStatement limit.
Hope it will be usefull for you.
my scenario is also somewhat similar,i have created a product and a pricebookEntry for the same but
i get an error no standard price defined for your product