You need to sign in to do that
Don't have an account?

How to access parent field in visualforce
I'm creating a visualforce email template but I am having a hard time accessing fields from a parent object. Currently I have the visualforce template related to the Opportunity object. I would like to access the Account name on the opportunity. How would I do this? Any example?
you need to reference the relationship name...
Account is the relationship name on the Opp.
so.... Opportunity.Account.Name
custom objects are more like so...
Ex:
Parent__c = is parent Object
Child__c = is child Object
Child__c.ParentId__c = API name of lookup/MasterDetail to parent
Child__c.ParentId__r = related Parent__c object
Child__c.ParentId__c = Id of Parent
Child__c.ParentId__r.Id = Id of Parent
Child__c.ParentId__r.Name = Name of Parent
All Answers
you need to reference the relationship name...
Account is the relationship name on the Opp.
so.... Opportunity.Account.Name
custom objects are more like so...
Ex:
Parent__c = is parent Object
Child__c = is child Object
Child__c.ParentId__c = API name of lookup/MasterDetail to parent
Child__c.ParentId__r = related Parent__c object
Child__c.ParentId__c = Id of Parent
Child__c.ParentId__r.Id = Id of Parent
Child__c.ParentId__r.Name = Name of Parent
Thanks for the help. For anyone else who may be needing the same thing, this is the code I used for the email template. The relatedTo object is Opportunity
<apex:outputField value="{!RelatedTo.Account.Name}"/>
Thanks for the help but now I've run into another problem along the same line. I'm trying to access the child object from a parent of Opportunities. Basically I'm making the email template in Opportunities and I want to access a field under a object called piccpqbms__dsPayment__c. The piccpqbms__dsPayment__c object is a child of Accounts. I've tried writing the following:
<apex:repeat value="{!RelatedTo.Account.piccpqbms__dsPayment__c.piccpqbms__paymentAmount__c}" var="pay">
but it says it is an invalid field, which makes sense because the dsPayment object is not a field on Accounts it is a child object. So how should I write this to tell visualforce that I want to go from the Opportunities Object > Accounts > piccpqbms__dsPayment__c > and access the piccpqbms__paymentAmount__c field?
In setup...
go to the account lookup/MD field on object piccpqbms__dsPayment__c and get the Child Relationship Name, if one does not exists create one.
I will call it 'MyPaymentRecords'
<apex:repeat value="{!RelatedTo.Account.MyPaymentRecords__r}" var="pay" >
{!pay.piccpqbms__paymentAmount__c}
</apex:repeat>
I get this error:
Error: Invalid field Payments__r for SObject Account
I double checked spelling and everything looks right. Went to the object Payments > Clicked on Account Name (which is the master detail lookup field) and the Child Relationship Name is Payments. So I typed the following:
<apex:repeat value="{!RelatedTo.Account.Payments__r}" var="pay" > <apex:outputField value="{!pay.piccpqbms__paymentAmount__c}"/> </apex:repeat>
and I get the error I listed. What am I missing?
try:
<apex:repeat value="{!RelatedTo.Account.piccpqbms__Payments__r}" var="pay" > <apex:outputField value="{!pay.piccpqbms__paymentAmount__c}"/> </apex:repeat>
Well, I tried the new code and now I'm getting another error but I'm not really sure what this one means:
I used the following code:
<apex:repeat value="{!RelatedTo.Account.piccpqbms__Payments__r}" var="pay" > <apex:outputField value="{!pay.piccpqbms__paymentAmount__c}"/> </apex:repeat>
execute this command from system log (link on top of page) to show/verify your relationshipname.
system.debug( Account.SObjectType.getDescribe().getChildRelationships());
Tesli,
You're a genius. I took Account out of the code and now it works. It doesn't make sense to me why because the payments object says it has a master-detail relationship with Accounts. But regardless it works. Thanks for the help.
I am in exactly the same situation. I'm creating a template from tma__Participants__c object, and try to access the parent called tma__Classes__c and the child relationship is tma__ClassDates__r.
I got an
Error: Unknown property 'String.tma__ClassDates__r' message
The code looks like this: <apex:repeat value="{!relatedTo.tma__Class__c.tma__ClassDates__r}" var="cd">
The strange thing is that I was able to see this child relationship when I expand the child relationships in Apex Explorer, however when I run your system log code it did not return the child relationship
system.debug( tma__Classes__c.SObjectType.getDescribe().getChildRelationships());
Your answer is only working for lookup field and not to md field.if u have another option please share in this page.
Thanks