+ Start a Discussion
huskerwendyhuskerwendy 

@IsTest(SeeAllData=false) doesn't allow me to query products

I created a test class in API version 32 that is trying to query the Product2 to get the Product Name. It doesn't work unless I set SeeAllData=True. In my old classes, I can query the products table directly.

I thought that according to this we can set it to false again and still be able to get the products.
https://success.salesforce.com/ideaView?id=08730000000j9TpAAI

Here's my query. 
Product2 prod = [select Id, Name, Included_Components__c, HasVersadoc__c from Product2 where Name =: productName];

Why doesn't that work unless I set "SeeAllDate=True"? 


Best Answer chosen by huskerwendy
UU
Hi huskerwendy

I also had this problem recently. You can access to standard pricebook id by Test.getStandardPricebookId();
Id pricebookId = Test.getStandardPricebookId();
Please refer to
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_test.htm#apex_System_Test_getStandardPricebookId

Hope you had already fixed your problem.

Cheers,


 

All Answers

Deepak Kumar ShyoranDeepak Kumar Shyoran
When you used (SeeAllData = false) in test class that means your organization data only records which exists in you org is no longer visible to your Test class you can use Object Schema. So to get a records from any Object first you have to insert it in database and then need fire SOQL query to retrieve that.

Test data which creates in test class only exists during the context of Test class execution.


huskerwendyhuskerwendy
Hi Deepak,

According to this idea, starting in the Summer 2014 release you should be able access the standard price book with that setting = False. Does that mean that you can access the price book but not the product? I'm confused. 

https://success.salesforce.com/ideaView?id=08730000000j9TpAAI
I'm happy to announce that coming in the next release (Summer 2014), you will be able to access standard price book in your apex tests using @isTest(seeAllData=False) annotation.

Deepak Kumar ShyoranDeepak Kumar Shyoran
Yes that mean you can assess the Standard Price book which was previously not possible with setting @Test(seeAllData=false) but for product you have to create them first before assessing in Test class with case seeAllData = false.
UU
Hi huskerwendy

I also had this problem recently. You can access to standard pricebook id by Test.getStandardPricebookId();
Id pricebookId = Test.getStandardPricebookId();
Please refer to
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_test.htm#apex_System_Test_getStandardPricebookId

Hope you had already fixed your problem.

Cheers,


 
This was selected as the best answer
huskerwendyhuskerwendy
Thanks, U. That is exactly what I was looking for. It's been a while, so I'll have to go look at my code, but I think I just set See All Data = True to get my test class to work. I'll implement the solution above though.