4

Overview: "No such column" error presents itself when I wrap valid SOQL in Apex Class. I am familiar with what this error means, but in this situation I don't feel as though the error is justified.

1) The following operations work exactly as expected and return rows:

Query editor:

Select Id, AccountId, Territory2Id from Opportunity

Execute Anonymous:

List<Opportunity> oList = new List<Opportunity>([Select Id, AccountId, Territory2Id from Opportunity]);

2) This is where it gets weird. When I copy paste my Apex from execute anonymous console and wrap in a dead-simple class, the save is not allowed.

Attempt to create a dead-simple Apex Class:

public with sharing class SomeClass {
    List<Opportunity> oList = new List<Opportunity>([Select Id, AccountId, Territory2Id from Opportunity]);
}

3) Upon attempting to save the class, I observe "No such column 'Territory2Id' on entity 'Opportunity'. 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."

Recap: FLS is fine, functional SOQL query proves the existence of the column, and Execute Anonymous proves the validity of the Apex. What could be the issue, what am I missing?

Thanks for any insights.

  • 2
    what version is your class? Territory2Id was a reasonably recent addition to the schema – cropredy Feb 10 '16 at 1:56
  • Thank you @crop1645, this was exactly the issue... I bumped up the API version and compiled fine. – krigi Feb 10 '16 at 2:06
3

Territory2Id is indicative of Salesforce Territory Management Version 2.0 that was released in Summer 14 as beta and apparently in PROD as Spring 15 so your class version needs to be at least V33 - thanks to @DasUber for pointing this out

The reason it worked in execute anonymous and query editor in Developer Console is that tool uses the current version of your org (which would be V36 = Spring 16)

  • Thanks again @crop1645 for pointing this out. One clarification about API versions: turns out this won't compile using v31 or v32, has to be v33+. I'll accept your answer once you update your answer so we don't confuse other readers :) – krigi Feb 10 '16 at 19:25
  • I had the same issue where one class (v38) would prepare a query for me and then the executing class was on v31 and could not execute the query, throwing this error... Thanks for pointint that out! – Willem Mulder Apr 13 '17 at 9:32

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.