+ Start a Discussion
OpsterOpster 

Apex Test class query returns no results in Sandbox

I have a very simple query that I have used in a test class to test a trigger.  This same code works in many other orgs, but for some reason returns zero results in one sandbox I am working in.

 

t.WhoId = [SELECT id FROM Contact LIMIT 1].id;

 

I see thousands of Contacts in the Sandbox and have tried even creating a new one.

 

Does anyone know why the code would return zero results and what I should do to fix it?

Best Answer chosen by Admin (Salesforce Developers) 
TheIntegratorTheIntegrator

In test classes you will need to create your own test data

 

put this line before the query and try again

 

Contact test_contact = new Contact(FirstName='Orig First Name',LastName='Orig Last Name',Email='orig@email.com');
insert test_contact;

All Answers

TheIntegratorTheIntegrator

In test classes you will need to create your own test data

 

put this line before the query and try again

 

Contact test_contact = new Contact(FirstName='Orig First Name',LastName='Orig Last Name',Email='orig@email.com');
insert test_contact;

This was selected as the best answer
OpsterOpster

Your suggestion of course worked.  But I am curious why my tests never failed in the past without inserting my own data for the test to run off of?  Any ideas there?

TheIntegratorTheIntegrator

I could give you some clues if you can give me an example from your past code. For this issue, if you could mark it as solved, it will help others find solutions.

Starz26Starz26

Opster wrote:

Your suggestion of course worked.  But I am curious why my tests never failed in the past without inserting my own data for the test to run off of?  Any ideas there?


Because you rprevious test methods use API < 24. In API v24 it changed