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
TeamFuguTeamFugu 

SOQL Statement Not Executing When Called From Test Class

I've run into problems where SOQL queries are not returning data when I'm running a test. I can copy the query string into either the Developer Console or using the Force.com IDE and the query returns the data. I see in documentation where the sample code for testing has a query in it so I think it should work. Do I need to elevate the user for the test or something? Or is this normal?

Best Answer chosen by Admin (Salesforce Developers) 
timdionnetimdionne
Make sure you include @IsTest(SeeAllData=true) to your test class.

All Answers

timdionnetimdionne
Make sure you include @IsTest(SeeAllData=true) to your test class.
This was selected as the best answer
sfdcfoxsfdcfox
By default, testing runs in isolation mode (since API 25.0). This reduces the possibility of a class of errors that come up when a test method runs into data it wasn't expecting because of live data.

If you need the query to return live data, use @isTest(SeeAllData=true). This is not recommended; your test methods should create any records they need to operate correctly from scratch, and leave SeeAllData as false. This reduces false errors and helps isolate your code's logic from external data.