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
Will LylesWill Lyles 

SOQL returns 0 rows when executed in a test class

I am trying to get some information about a specific user in order to run a test against my code.  The first query runs fine and I can get the associated Contact Id for the user, but when I try to get the Oracle_Account_Number__c for the contact in the second query it returns 0 rows.  If I copy the SOQL select statement from the debug logs and run it manually it returns the correct information, so the syntax seems to be correct.

Does anyone see any issues or know why I'm getting no results on the second query?
@isTest
public class myTestMethod {

    static testmethod void testupdateLineItem()
    {     
        User u = [SELECT Id FROM User WHERE UserName='community.test1@example.com'];
        System.runAs(u)
        {
            ApexPages.StandardController sc = new ApexPages.StandardController(new Case());
  			m_Case_Details_Apex test = new m_Case_Details_Apex(sc); 
     
            //get the contact ID based on the named user
            string curUserNameQry = 'SELECT ContactId FROM User where username = \'' + userinfo.getusername() + '\'';
            User curContactId = Database.query(curUserNameQry);
            
            //get the Oracle Account number associated with the Contact ID
            string oracleAcctNumQry = 'SELECT Id,Oracle_Account_Number__c from Contact where Id = \'' + curContactId.ContactId + '\'';
            Contact oracleAcct = Database.query(oracleAcctNumQry);
            string oracleAcctNum = oracleAcct.Oracle_Account_Number__c;  
        }        
    }

 
Best Answer chosen by Will Lyles
DimondDimond
@isTest(SeeAllData=true)

All Answers

DimondDimond
@isTest(SeeAllData=true)
This was selected as the best answer
Will LylesWill Lyles
Wow, that worked! Thanks Dimond.  That was almost too easy. :)
I didn't think I needed that since it was returning the user information without it and the permissions on the contact field were set correctly.  
DimondDimond
no prob. but as best practic, you prob shouldn't rely on it. you should created your own test data, it will save you a ton of headache down the road.