Thursday 6 September 2012

Display Fields from Related Records - Visualforce

You can also display data from related records. For example, while viewing the object details for Account, you might have noticed that the Account object has a field called Account Owner, and that its type is Lookup(User). In other words, this field has a relationship to a User record. By clicking the Account Owner field label link, you’ll discover its Field Name is Owner.
The Owner relationship represents a User. And, if you click Customize | Users | Fields, you’ll find that User has a Name field. Let’s use this information to display it.
  1. Modify accountDisplay to include this field by adding the following paragraph after the existing one:
    <p>Here's the owner of this account: {! account.Owner.Name}</p>
The “dot notation” (account.Owner.Name) indicates that you want to traverse the relationship between the records. You know that account.Owner indicates the Owner field of the account record. The extra name at the end indicates that the owner field isn’t a simple field representing a String, but a relationship to another record (it’s a Lookup(User)), and that you’d like to get the record represented by the value of the Owner field (it’s a User record), and display the Name field on that record.
Tip
If you’ve created your own custom objects (instead of using objects like Account) and want to know how to reference a field, you have to follow a slightly different procedure. ClickSetup | Create | Objects, select your object, and then the field. The API Name now indicates the name of the field that you must use in your Visualforce pages. For example, if your field was called Foo, its API Name is Foo__c, and you’d reference it with that name—something like: {! myobject__c.foo__c}.

1 comment:

  1. Shouldnt {! myobject__c.foo__c} actually be {! myobject__r.foo__c}. signifying the relationship name?

    ReplyDelete