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
Bryan Leaman 6Bryan Leaman 6 

Why is it this soql query works in query editor but not in apex

I have a simple SOQL query that I can run using developer console or workbench and it runs fine. But when I copy/paste it into anonymous apex it complains the the field I'm requesting does not exist.  How can that be?  I am running this as a system admin and I *do* have authority to the field. 

Here's a screenshot showing the query in developer workbench side-by-side with anonymous apex and the results.

SOQL Query vs Apex soql query
 
Bryan Leaman 6Bryan Leaman 6
Those screen-shots don't seem to be coming through well...
SOQL query editor -- works:
select Id, Name, dealer__Region__c  
from dealer__Dealer_Location__c 
where dealer__Region__c='Florida'

Anonymous Apex -- doesn't like the field dealer__Region__c:
System.debug([
    SELECT Id, Name, dealer__Region__c 
    FROM dealer__Dealer_Location__c 
    where dealer__Region__c='Florida'
]);
Results in "Execute Anonymous Error" box:
Line: 1, Column: 14
SELECT Id, Name, dealer__Region__c ^ ERROR at Row:2:Column:22 No such column 'dealer__Region__c' on entity 'dealer__Dealer_Location__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

 
Jacob Elliott 8Jacob Elliott 8
Hey Bryan, this is probably a dumb question, but it looks like the SOQL is the same, so I'm going to ask it. :-) Are you sure Workbench is pointing to the correct environment?

If you try "SELECT Id FROM dealer__Dealer_Location__c" in Salesforce does it show anything? If so, slowly add the other columns back in and then the filter.

Try typing out the field names instead of copy/paste if you previously did that.

Lastly, if it still isn't working, I would double-check to make sure the field does exist in the environment you are testing in (I know you've probably already done this, just covering bases :-) ).
Bryan Leaman 6Bryan Leaman 6
Thanks for the reply.  They're both the same environment. I get the same error using developer console using the same soql queries. If I omit the "dealer__Region__c" field it works. 

It seems this fails any time we have a custom field named the same as a managed field in the object (apart from the namespace prefix) -- I can't query the managed field name in Annonymous Apex, but I can query it using the query tools of developer console and workbench. 

I've tried different API levels in workbench and it seems to act the same. I've never seen this before, but it's just slightly possible that I'd not tried it before.  Our managed package provider created custom fields for us and then later added the same field to the managed package. I haven't seen any issues in regular code -- classes, triggers, VF, lightning -- so far just in anonymous apex.

This is *so* bizarre!