2

I'm trying to query for a custom object.

In the query editor in the Developer console, this query returns 1 row.

SELECT Id, Name FROM Household_Served__c LIMIT 1000

But then when I code it in Apex, the list is empty.

List<Household_Served__c> h = [SELECT Id, Name FROM Household_Served__c LIMIT 1000];
System.debug(h.size());

Why is the editor returning a row, but in Apex it is not returning anything?

  • Where is the Apex being invoked? Is it being run from a test? – Mark Keats Mar 27 '15 at 14:00
  • I'm assuming that you are running the query in the Developer Console as the code using the same user? – Robin Wijnen Mar 27 '15 at 14:00
  • It is being run from a test. And yes, that is correct – TemporaryFix Mar 27 '15 at 14:01
5

Unless you specify the seealldata=true attribute (which I don't recommend) then the test will not be able to access live data. You would need to create the custom object record(s) within your test, or inside a @testsetup method.

  • I'm fairly new to salesforce development and I was trying to avoid this because the object has so many relationships. – TemporaryFix Mar 27 '15 at 14:05
  • Your test should set-up any data that it needs within it. The seealldata attribute would be a workaround, but I personally wouldn't recommend it. – Mark Keats Mar 27 '15 at 14:06
  • @JoeyThomaschaske More to the point, by depending on SeeAllData, your package won't be easily portable to other organizations (e.g. new Sandboxes, possibly Production, and so on). Make your test data in your unit test so you can avoid complications later. I personally use a class called TestData that creates all the relationships in order, allowing me to re-use the logic across many (or all) of my tests. – sfdcfox Mar 27 '15 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.