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
case commentscase comments 

List has no rows for assignment - Pricebook2

I'm trying to set up some test code to insert 200 products.  When I run the test code, I get an error stating: List has no rows for assignment.  This is occuring on the code for:
Pricebook2 pbook = [Select id from Pricebook2 where isStandard = true];

The weird thing is, I copied and pasted the exact same code to execute anonymous and it ran fine, but running it through test coverage errors out.  Am I missing something here?
@isTest

public class AfterOppTriggerTest {
    static testMethod void OppTest(){
        //create a list of products       
        List<Product2> productstoinsert = new List <Product2>();
        for(Integer x = 0; x < 200 ;x++){
            Product2 newprod = new Product2(Name = 'newprod' + x, isactive = true);
            productstoinsert.add(newprod);
          
        }
        insert productstoinsert;
        //Select the pricebook
        Pricebook2 pbook = [Select id from Pricebook2 where isStandard = true];
        system.debug(pbook);
        
        //create pricebook entries
        List<PriceBookEntry> pbinsert = new List<PriceBookEntry>();
        for(Integer x=0; x < 200; x++){
            PriceBookEntry pbentry = new PricebookEntry(Pricebook2ID = pbook.Id, Product2ID = productstoinsert[x].id, unitprice = 100, isactive = true, usestandardprice = false);
            pbinsert.add(pbentry);
            system.debug(pbentry);
        }
        insert pbinsert;
        
        
    }
}


Best Answer chosen by case comments
AshlekhAshlekh
Hi,


Use
Test.getStandardPricebookId()

method instead of below line
Pricebook2 pbook = [Select id from Pricebook2 where isStandard = true];



Because by query you cann't get the id.Salesforce 14 summar provide this new method.


IF it helps you than please mark it as a solution and ENJOY APEX

All Answers

Boris BachovskiBoris Bachovski
You need to annotate your class with
@isTest(SeeAllData=true)
Your test class doesn't have access to existing data by default unless explicitly specified.
James LoghryJames Loghry
Boris, while this was true in prior versions, in summer '14 (api Version 31.0) you can now create your own pricebooks in unit tests!  Therefore, there is no need in this case to rely on the SeeAllData=true annotation.  In your unit test, you'll need to create your own standard price brook.  Follow the example on this blog for some guidance.  http://panaya.com/blog/salesforce/summer14-pricebook-enhancements/
Boris BachovskiBoris Bachovski
It wasn't specified which API version is used in the question, I assumed it's not Summer '14. Totally agree that SeeAllData must be avoided when on Summer '14 since we have the ability to create pricebooks. :)
AshlekhAshlekh
Hi,


Use
Test.getStandardPricebookId()

method instead of below line
Pricebook2 pbook = [Select id from Pricebook2 where isStandard = true];



Because by query you cann't get the id.Salesforce 14 summar provide this new method.


IF it helps you than please mark it as a solution and ENJOY APEX
This was selected as the best answer
case commentscase comments
Thanks all for the quick responses!