1

I have a Visualforce page with a standard controller Account. I want to access a rich text field in a related Contact. Below is what I tried:

<apex:outputText value="{!Account.Contacts__r.rich_text_field}"  escape="false" />

However it is giving me the error below:

Invalid field Contacts__r for SObject Account

My understanding is since for 1 Account you can have many Contacts i.e. one to many relationship, and since I am at the Account level, I can use the 'Child Relationship Name' on the lookup field in Contact to traverse to the child object (Contact) and select the Contact field I need.

Is this wrong? I am unsure why I have this error.

  • @M guy, your thoughts are correct, and I will give you a tip. Lets imagine you have 2 related contacts to account. What then? Which rich text field will be displayed? from first ot second contact? – Oleksandr Berehovskyi Jun 14 '18 at 9:47
2

My understanding is since for 1 Account you can have many Contacts i.e. one to many relationship

This is the exact reason why you can't access child record from parent as they could be many and system doesn't know which one to select.

Account.Contacts returns a list(__r is for custom objects ) and you can't access any field from a list without going to a particular record.

Either you can iterate the child records or have query on child object querying parent fields in it.

| improve this answer | |
  • I thought you use __r when you want to traverse relationships. When do you use/ do not use __r? – M guy Jun 14 '18 at 9:53
  • @Mguy See my updated answer. – Mr.Frodo Jun 14 '18 at 9:56
  • Does that mean for any lookup, you can use Object.relationship and it returns a list. From here you would index and select the fields required? – M guy Jun 14 '18 at 9:59
  • @Mguy Yes this is exactly what needs to be done. – Mr.Frodo Jun 14 '18 at 10:02
  • Got it. Think I need a little more practice with relationships but thank you! – M guy Jun 14 '18 at 10:05

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.