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
EldonEldon 

Cant access pricebook2.isstandard from my test class

Hi

I am trying to query my standard pricebook in my apex method by following code,

PriceBook2 PB=[select id,Name from Pricebook2 where IsStandard = true];

Im getting the result in my application but when i write test class and call the method containing that code im getting the following error,

System.QueryException: List has no rows for assignment to SObject.

finally i did my test class using seealldata=true and im able to get the results

What is the reason for this?

 
 
Dilip_VDilip_V
Hi Eldon,

1.We no need to use (seeallData=true) after summer 14 release.
2.Make sure that your test class API version is latest(37).
Use below code as a reference
 
@isTest
public class PriceBookTest {
    // Utility method that can be called by Apex tests to create price book entries.
    static testmethod void addPricebookEntries() {
        // First, set up test price book entries.
        // Insert a test product.
        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;
        
        // Next, perform some tests with your test price book entries.
    }
}

for more info 

https://releasenotes.docs.salesforce.com/en-us/summer14/release-notes/rn_apex_price_books_in_tests.htm

If it helps make it as best answer.

Thanks.
EldonEldon
Hi Dilip,

I am using Test.getStandardPricebookId(); in my test class.
The problem is when i call my apex method from my test class the following code in my apex method is not working.

PriceBook2 PB=[select id,Name from Pricebook2 where IsStandard = true];

We can use test.getStandardPricebookId(); only in our test class. My req is that i want to use the standard pricebook id in my apex method. Hope you understood the problem.
Dilip_VDilip_V
Eldon,


Your query may returns more than one row.Try with limit like this.
PriceBook2 PB=[select id,Name from Pricebook2 where IsStandard = true limit 1];

Thanks.
EldonEldon
It is not returning any rows. The error is,
System.QueryException: List has no rows for assignment to SObject.
 
Azeez ThummalapalliAzeez Thummalapalli
I am also having the issue as Eldon. I have tried using both of the below ways in Test class
Id pricebookId = [Select Id From Pricebook2 where IsStandard=true limit 1].Id;
and
Id pricebookId = Test.getStandardPricebookId();
But, no luck. Can some help regarding this.
 
EldonEldon
Hi Aneez,

What i did is i used a custom pricebook in my app. If you want to see the std PB you will have to use seealldata=true in your test.

Regards
Azeez ThummalapalliAzeez Thummalapalli

Yes Eldon, I agree with you.
But in Summer'14 Release Salesforce stated that 'You can create price book entries for standard and custom price books in Apex tests.' If we cannot access standard pricebook without seealldata = true, then what is the purpose of adding this feature. May be Salesforce team have to made some amendments to this feature, otherwsie they should convey the information properly in Release Notes.

Anaways, Thank you for your suggestion.

If someone who does not wnat to put seealldata = true, they can try using Test.isRunningTest() if block and read the normal pricebook values just to cover the code.

Thak you.

Suraj Tripathi 47Suraj Tripathi 47
Hi Eldon,
You can fetch Standard Pricebook in test class by 
this way-:
Pricebook2 standardPricebook = new Pricebook2(Id =        Test.getStandardPricebookId(),IsActive = true);
update standardPricebook;

If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi