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

Standard pricebook is not found in test class
In my test class I insert new products and pricebookentries, in my test class I don't use seeAllDate=true annotation and in order to retrieve standard pricebook id I used Test.getStandardPricebookId() method, here's my code:
pbes.size() returns 0. I used the same query in console for existing data and I got results. What am I doing wrong?
Product2 p1 = new Product2(Name='Product Monthly 1111', Family='Monthly', isActive=true, CurrencyIsoCode='USD'); Product2 p2 = new Product2(Name='Product Yearly 2222', Family='Yearly', isActive=true, CurrencyIsoCode='USD'); insert new List<Product2>{p1, p2}; Id pricebookId = Test.getStandardPricebookId(); PricebookEntry pbe1 = new PricebookEntry(Pricebook2id=pricebookId, Product2id=p1.ID, isActive=true, CurrencyIsoCode='USD', unitPrice=100); PricebookEntry pbe2 = new PricebookEntry(Pricebook2id=pricebookId, Product2id=p2.ID, isActive=true, CurrencyIsoCode='USD', unitPrice=50); insert pbe1; insert pbe2; List<PricebookEntry> pbes = [SELECT ID FROM PricebookEntry WHERE Product2id IN (:p1.Id, :p2.Id) AND Pricebook2.isStandard = TRUE AND Pricebook2.isActive = TRUE AND CurrencyIsoCode = 'USD' AND isActive = TRUE]; System.assertEquals(2, pbes.size());
pbes.size() returns 0. I used the same query in console for existing data and I got results. What am I doing wrong?
Please create PriceBookEntry and Pricebook2 record in your test class. like below :-
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
thanks for your reply. If you looked at my code, I created everything you mentioned and have the same structure as you have. I want to use only standard pricebook and not custom, I used
to get id of standard pricebook. But when I do a query I have no results returned. Maybe it's a problem with a query?
Eg...
It's friday and everyone sensible have left, apart from me beating this dead horse. Time to lock the office. Thanks!
Please get this marked for correct solution.
You can fetch Standard Pricebook in test class by
this way-: If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi
you need to update the Standard pricebook in @isTest class-
UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: