Find your content:

Search form

You are here

Confused over why Ids are acting as strings rather than references

 
Share

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


Attribution to: Joe

Possible Suggestion/Solution #1

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}" />

Attribution to: JCD
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1692

My Block Status

My Block Content