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
Jeff_Rogers.ax1383Jeff_Rogers.ax1383 

SOQL Issue In Trigger Test Class

I'm trying to enter a new pricebookentry into the standard price book (which is inactive), but I can't get any results querying for the Standard Price Book (planning on setting inactive to false) through the test class.  Here's the error I get:

 

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

 

And here is the query:

PriceBook2 pbStd = [SELECT Id, IsActive FROM PriceBook2 WHERE Name = 'Standard Price Book'];

 

When I run the same query in the Force IDE I get the record.

 

Any thoughts?  


Best Answer chosen by Admin (Salesforce Developers) 
Jeff_Rogers.ax1383Jeff_Rogers.ax1383

Adding the annotation (seeAllData=true) to the end of @isTest AND activating/deactivating the Standard Price Book (weird) allowed me to get my test class to work as desired.  Thanks for all the suggestions!

All Answers

doubleminusdoubleminus

Ensure you aren't running the test class query as a User who would not have permission to access those pricebook records.

 

Also, try the IsTest(SeeAllData=true) annotation for your test, if it's API version 25.0.

Jeff_Rogers.ax1383Jeff_Rogers.ax1383

Thank you for the reply.  I'm running the test under my account which has access to the standard price book.  Also, I added the "SeeAllData" annotation and still did not get any results from the query.  Are there no results because the standard price book is inactive?  I wouldn't think that would be the case since I get results when I run the query through the Force IDE.  Any other ideas?

Jerun JoseJerun Jose

Hi Jeff,

 

You should be able to query from the Pricebook even though it is inactive. I generally query the standard pricebook using the query below.

[select id from Pricebook2 where IsStandard = true];

 Hope it helps.

 

Thanks,

 

Jerun Jose

 

 

Jeff_Rogers.ax1383Jeff_Rogers.ax1383

Adding the annotation (seeAllData=true) to the end of @isTest AND activating/deactivating the Standard Price Book (weird) allowed me to get my test class to work as desired.  Thanks for all the suggestions!

This was selected as the best answer