In this article (4 sections)
Stage: tax calculation on invoice lines. The project was building an Apex tax engine adapter that looked up the rate for a postal code and a product code. Both fields existed on the rate object, with data in them, visible in the UI and in the object describe. The query inside the class kept answering No such column.
The first fix tried
The obvious suspect is the query. The next step is usually to rebuild it as a string, with dynamic SOQL, to get around whatever the compiler dislikes. It changes nothing, and that's what makes this one expensive: two attempts, the same error, and a field that clearly exists.
Why the class can't see the field
Every Apex class is saved with an API version, and it sees the data model as it was in that version. Those two fields exist from API 66.0 onward. For a class saved on an earlier version, they don't exist, whatever the org shows elsewhere. Dynamic SOQL runs under the same class, so it gets the same answer. The fix was to save the class on API 66.0 or later.
The signal to recognize is a field that exists everywhere except inside one class. The same error has a second common cause: a freshly deployed custom field stays invisible to SOQL and Apex until field-level security is granted, which looks just like a failed deployment.
In practice
- When a field is visible everywhere except in the code, open the class's metadata and check its API version before touching the query.
- Grant field-level security right after deploying a field, before testing any query.
- When a class starts using newer fields, raise its API version in the same change, and rerun its tests.