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
Behzad Bahadori 18Behzad Bahadori 18 

Method does not exist or incorrect signature: Test.getStandardPricebookId() API Version 31.0

I am writing a test class and I cant use Test.getStandardPricebookId()   , and if I use @isTest (SeeAllData = true)   Ill get the error that 
STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: [] 

Here is the test case
 
Product2 prod = new Product2(Name = 'Laptop X200', 
            Family = 'Hardware');
        insert prod;
        
        // Get standard price book ID.
        // This is available irrespective of the state of SeeAllData.
        Id pricebookId = Test.getStandardPricebookId();
        
        // 1. Insert a price book entry for the standard price book.
        // Standard price book entries require the standard price book ID we got earlier.
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        // Create a custom price book
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
        
        // 2. Insert a price book entry with a custom price.
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;


             QuoteLineItem qli = New QuoteLineItem();
             qli.Product2Id=prod.id;
             qli.UnitPrice=12;
             qli.Quantity=50;
             qli.QuoteId=qot.id;
             qli.UnitPrice=50;
             qli.PricebookEntryId=customPrice.id;
             insert qli;

 
Best Answer chosen by Behzad Bahadori 18
scottbcovertscottbcovert
Hi Behzad,

I thought with Summer '14 you should be able to use the Test.getStandardPricebookId() method but if that isn't working then you could set SeeAllData to true and then replace line 7 with the following:
 
Pricebook2 standardPbook = [select id from Pricebook2 where IsStandard = true limit 1];
Id pricebookId = standardPbook.Id;

Here are some additional resources that might prove useful:

https://developer.salesforce.com/forums/?id=906F00000008yveIAA

http://stackoverflow.com/questions/9164986/how-do-i-avoid-standard-price-not-defined-when-unit-testing-an-opportunitylineit

All Answers

scottbcovertscottbcovert
Hi Behzad,

I thought with Summer '14 you should be able to use the Test.getStandardPricebookId() method but if that isn't working then you could set SeeAllData to true and then replace line 7 with the following:
 
Pricebook2 standardPbook = [select id from Pricebook2 where IsStandard = true limit 1];
Id pricebookId = standardPbook.Id;

Here are some additional resources that might prove useful:

https://developer.salesforce.com/forums/?id=906F00000008yveIAA

http://stackoverflow.com/questions/9164986/how-do-i-avoid-standard-price-not-defined-when-unit-testing-an-opportunitylineit
This was selected as the best answer
Behzad Bahadori 18Behzad Bahadori 18
@scott 

thank you for the respond . yes I dont understand why its not working with my API ; now it says FIELD_INTEGRITY_EXCEPTION, The pricebook entry is in a different pricebook than the one assigned to the Quote, or Quote has no pricebook assigned
scottbcovertscottbcovert
Behzad Bahadori 18Behzad Bahadori 18
Thank you Scott
scottbcovertscottbcovert
No problem Behzad, happy coding!