function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhijeet Purohit01Abhijeet Purohit01 

Problem with test class.

I am trying to run a test class and I got this error message. What does it mean?

 

The problem here is there is no such  field with the API name as Standard Price or there is no validation rule with message as STANDARD_PRICE_NOT_DEFINED.

 

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

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

If you create a product2 record, you must create a pricebookentry record where pricebook2id is the ID of the standard price book with some value. You must use @isTest(SeeAllData=true) to query this ID value.

All Answers

sfdcfoxsfdcfox

If you create a product2 record, you must create a pricebookentry record where pricebook2id is the ID of the standard price book with some value. You must use @isTest(SeeAllData=true) to query this ID value.

This was selected as the best answer
Abhijeet Purohit01Abhijeet Purohit01

Yes. Exactly. This is the error. Can you please elaborate. I have done...

@isTest(seeAllData=true) but still same.

 

sfdcfoxsfdcfox
@isTest(SeeAllData=true)
private class testclass {
  static testmethod void test( ) {
    product2 prod = new product2( name='test',isactive=true );
    insert prod;
    pricebook2 standardpb = [select id from pricebook2 where isstandard = true];
    pricebook2 custompb = new pricebook2(name='test',isactive=true);
    insert custompb;
    pricebookentry standardprice = new pricebookentry( product2id = prod.id, pricebook2id = standardpb.id, unitprice = 4.99), customprice = new pricebookentry( product2id = prod.id, pricebook2id = custompb.id, usestandardprice = true );
    insert standardprice;
    insert customprice;
  }
}

 

SFDC HedgehogSFDC Hedgehog
What if our standard price book is deactivated?
Is there a way to write a test class without the seealldata=true  declaration?