1

I am getting this error in my test class with pricebook entries and products:

System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []

I've seen many solutions using a query for standard pricebook, but I don't want to use that, because SeeAllData needs to be true for it to return anything, and that messes up the rest of the test class. Suggestions?

ID standardPBID = Test.getStandardPricebookId(); 
    List<Pricebook2> standardPB = [select id from Pricebook2 where isStandard=true];
    System.debug('Standard Pricebook found?');
    System.debug(standardPB.size() > 0); // false
    Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
    insert customPB;

    Product2 elderProd = new Product2(IsActive = true, Name = 'Elder Gifts');
    insert elderProd;
    PricebookEntry elderPBE = new PricebookEntry(
        Pricebook2Id = customPB.Id, Product2Id = elderProd.Id,
        UnitPrice = 0, IsActive = true, UseStandardPrice = true);
    insert elderPBE;
6
  • Which insert is failing with this exception?
    – Adrian Larson
    Nov 14, 2016 at 14:59
  • the Pricebook entry. I thought that was obvious from the error. elderPBE Nov 14, 2016 at 15:03
  • Why are you using a custom Pricebook? Just set Pricebook2Id = standardPBID.
    – Adrian Larson
    Nov 14, 2016 at 15:06
  • I get the following: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: [] on the same line Nov 14, 2016 at 15:10
  • I can't tell which field isn't correct -- but standardPBID is an ID (used debug -- it's not null or anything) and elderProd.Id has an ID as well. Nov 14, 2016 at 15:14

1 Answer 1

1

Try the following code. Here you can see we insert a price book entry for the standard price book.

Product2 elderProd = new Product2(
        IsActive = true,
        Name = 'Elder Gifts'
        );
insert elderProd;

ID standardPBID = Test.getStandardPricebookId(); 

PricebookEntry standardPrice = new PricebookEntry(
        Pricebook2Id = pricebookId,
        Product2Id = prod.Id,
        UnitPrice = 10000,
        IsActive = true
        );
    insert standardPrice;

List<Pricebook2> standardPB = [select id from Pricebook2 where isStandard=true];
System.debug('Standard Pricebook found?');
System.debug(standardPB.size() > 0); // false

Pricebook2 customPB = new Pricebook2(
    Name='Custom Pricebook',
    isActive=true
    );
insert customPB;


PricebookEntry elderPBE = new PricebookEntry(
    Pricebook2Id = customPB.Id,
    Product2Id = elderProd.Id,
    UnitPrice = 0,
    IsActive = true
    );
insert elderPBE;

Error may be coming from following line:

UseStandardPrice = true

You must log in to answer this question.

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