Browsing salesforce topics on message boards I came across this code:

CaseComment comment = [SELECT CreatedById, CreatedBy.UserName FROM CaseComment];

It looks like CreatedBy is a field of CaseComment. However, CreatedBy doesn't appear as a field in the documentation for CaseComment nor is it listed in the documentation as a standard object.

Where is the documentation that covers the CreatedBy object?

  • Cory, be sure to accept whichever answer best answered your question, and upvote answers which were helpful.
    – zachelrath
    Dec 4 '12 at 22:38

There is no CreatedBy object --- the CreatedById reference field, along with LastModifiedById and OwnerId, is (except in rare cases) a reference to the User object. Thus, querying for CreatedBy.UserName on the CaseComment object is requesting the UserName field of the User record who created the CaseComment record.

If you were thrown off by the lack of a __r ending on the CreatedBy field to indicate that it is a reference to another object, you're not alone --- this is a common source of confusion for Force.com developers. Here's a quick reference to help you when dealing with Reference fields:

  • Standard Object reference fields
    • Always end in Id, e.g. CreatedById, ParentId
    • Can point to / reference multiple objects (polymorphism)
    • Fields on related objects can be accessed by removing the Id from the field name, e.g. CreatedBy.Email, Parent.Name
  • Custom Object reference fields
    • Always end in __c, e.g. Survey_Question__c
    • Will always point to a single object, e.g. to Survey_Question__c
    • Fields on related objects can be accessed by replacing the __c with __r on the field name, e.g. Survey_Question__r.Prompt__c

Hope that helps!


The System Fields documentation covers this (sort of):


CreatedBy is the relationship to the User identified by the CreatedById on the record.


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.