Catch fields referenced without being referenced in SOQL in the editor

Issue #1301 duplicate
Elie Hassan created an issue

When a field is referenced from a object that was pulled in via a SOQL query, is it possible to check the query to make sure that the field is actually included in the query? Catching this error in the IDE would save time.

Comments (3)

  1. Scott Wells repo owner

    Elie, I like the idea and have considered it for a while, but unfortunately to have it really work, you'd have to have visibility into all the ways that query results are used. For example, one relatively common practice is to encapsulate queries into a data access layer, e.g.:

    public Account getAccount(Id accountId) {
        List<Account> accounts = [SELECT Id, Name, AccountNumber FROM Account WHERE Id = :accountId LIMIT 1];
        return (accounts != null) && (accounts.size() == 1) ? accounts.get(0) : null;
    }
    

    Now IC would have to look at all callers of that method (transitively for completeness) and how the return value is referenced to identify when an unqueried field is also referenced.

    Dynamic SOQL also complicates this considerably, but I think it's fine to limit such an inspection to static SOQL queries.

    So my question is really whether something that only ever inspects usages within the current scope of the query execution itself is useful enough. Thoughts?

  2. Elie Hassan reporter

    Yes, I was thinking that it would have to be limited to static SOQL queries. I think it would be worthwhile even if it’s only within the scope of the query execution. That would catch a lot of potential issues at compile time instead of when testing the flow in SF or when running your unit tests.

    For the cases that are not covered by this, they would not be worse off than they are now.

  3. Log in to comment