2

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:

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?

UPDATE: When I debug this code, standard pricebook returns isStandard and isActive fields both with false values:

List<PricebookEntry> pbez = [SELECT ID, Product2Id, Pricebook2.isStandard, Pricebook2.isActive, CurrencyIsoCode, isActive FROM PricebookEntry];

system.debug(pbez[0].Pricebook2.isStandard);
system.debug(pbez[0].Pricebook2.isActive);

How is it possible to workaround this?

1 Answer 1

1

I believe the issue is (and it caught me for some time before) is that with SeeAllData=false, the Standard Pricebook doesn't actually exist but its ID does.

You need to change your query to:

List<PricebookEntry> pbes = [SELECT ID FROM PricebookEntry 
    WHERE Product2id IN :new List<ID> {p1.id,p2.id} AND 
    Pricebook2Id = :Test.getStandardPricebookId() AND 
    CurrencyIsoCode = 'USD' AND isActive = TRUE];

Of course, since you are using SeeAllData=false, simply doing:

System.assertEquals(2,[select COUNT() from PricebookEntry]);

will verify that you successfully inserted your PBE

3
  • Thanks for explaining, the query I showed in example I use in my trigger, so I can't really change it just for testing purposes, I will use SeeAllData=true then.
    – NGix
    Commented May 14, 2015 at 13:13
  • @lr0nm ohhhhhhh -- you should not have to ever use seeAlldata=true when testing pricebooks/pricebookentries. Your testmethod should be using asserts to verify the underlying trigger code and secondarily using asserts to verify proper testdata mocking (especially if there is some doubt over mock setup logic)
    – cropredy
    Commented May 14, 2015 at 16:40
  • Yep, I could actually find a better solution and modify my trigger logic: salesforce.stackexchange.com/questions/47222/…. But thanks for help
    – NGix
    Commented May 14, 2015 at 16:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .