7

I'm trying to traverse a relationship between the CaseComment object and the Case object to get the value of the Case's CaseNumber field.

<apex:outputText value="{!comment.ParentId.CaseNumber}" />

but I get the following error:

Error: Unknown property 'String.CaseNumber'

So, from this I see that Salesforce is treating ParentId as a String rather than a reference to the Case object, as I was hoping.

I note also that the 18 character version of the ID is returned if that makes any difference.

I'd like to know:

  • In what circumstances I can use a relationship such as this as a reference to another object, and in which circumstances it will merely be treated as a String?

Thanks a lot

1 Answer 1

15

Typically if there is a standard field that ends in "Id", you can access the reference (and thus the fields associated with that reference) by leaving off the suffix. For example, you can get the CreatedById value (which would be treated as an Id type in code that can be casted to a String, or simply a String if referenced via an apex:outputText tag), or the name of that user with CreatedBy.Name.

You can also use an apex:outputField tag on an Id (such as Opportunity.AccountId to display the name of an opportunity's parent account) to show the reference's Name field with a nifty built-in hover box (see here).

For this particular example, give this a shot:

<apex:outputText value="{!comment.Parent.CaseNumber}" />
4
  • Thanks for your explanation which is very helpful. Using the above merge expression, I am getting an error: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: CaseComment.Parent despite the fact that the SOQL query in the contoller is in fact selecting the ParentId field for the CaseComment. In case it makes any difference, my visualforce page is for a case object but I am accessing all the CaseComments that exist for this and related cases in a pageBlockTable.
    – Joe
    Commented Sep 26, 2012 at 15:09
  • 7
    @Joe, rather than querying ParentId, you'll want to query the Parent.CaseNumber field (or perhaps both, if you need ParentId for something else behind the scenes). Querying just ParentId won't pull the fields on that associated record from the database.
    – JCD
    Commented Sep 26, 2012 at 15:12
  • OK. That's great. Thank you. I'll have to spend a bit more time absorbing this and figuring out why it works this way.
    – Joe
    Commented Sep 26, 2012 at 15:38
  • 4
    The more general case of this situation is that any lookup relationship field also has a relationship name that goes along with it. The field ParentId has the relationship name Parent and AccountId has the relationship name Account. For custom fields, though, the relationship name is not constructed the same way; the __c is removed, and replaced with __r. So the field Primary_Contact__c will have a relationship name of Primary_Contact__r, and the Last Name of the primary contact is Primary_Contact__r.LastName. Commented Sep 26, 2012 at 18:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .