November 2, 2010

Understanding–“No such column on entity 'Name'” Error !

One can get errors like

“No such column 'SmallPhotoUrl' on entity 'Name'.” OR “No such column CompanyName' on entity 'Name'.”

on a SOQL statements like

  • SELECT CreatedBy.CompanyName, FeedPost.Body, Id, CreatedBy.Name FROM AccountFeed
  • SELECT CreatedBy.SmallPhotoUrl, FeedPost.Body, Id, CreatedBy.Name FROM AccountFeed
  • SELECT Id, FeedPost.CreatedBy.SmallPhotoUrl, (SELECT Id, CreatedBy.Name, CreatedBy.SmallPhotoUrl, CommentBody FROM FeedComments) FROM NewsFeed

The column responsible for this error is bold highlighted in above SOQL statements. The error is so confusing, you must be wondering that : “I queried on User(CreatedBy) relation, then why the field is expected to be their on entity ‘Name’ ?

Why this error is thrown for entity “Name” ?

Its because “CreatedBy” and many other Salesforce relations become “Polymorphic” if pointing to more than one type of object as parent. This is what Salesforce "Relationship Queries" doc says:

“A polymorphic key is an Id that can refer to more than one type of object as a parent. For example, either a contact or a lead may be the parent of a task. In other words, the WhoId field of a task may contain the Id of either a contact or a lead. If an object can have more than one type of object as a parent, the polymorphic key points to a Name object instead of a single object type.”

So, one can’t expect a single Sobject type fields for polymorphic relations/keys. Rather, check what fields are available in entity “Name”. That is why one can’t find User sobject’s fields like SmallPhotoURL and CompanyName on CreatedBy polymorphic relationships.

Note: Not all CreatedBy relationships are polymorphic it totally varies from sobject to sobject.

How to know if a relationship is Polymorphic ?

To know whether a relation is polymorphic, you can do either of following

  • User describeSObjects call, as described in section “Understanding Polymorphic Keys and Relationships” of Salesforce "Relationship Queries" doc
  • Use Eclipse > Schema browser and expand into the “relationship column > Type Data > Reference To”, as shown below : image