function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BshardiBshardi 

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?
Best Answer chosen by Admin (Salesforce Developers) 
TomSnyderTomSnyder

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

 

 

 

 

 

 

Message Edited by tesii on 03-23-2010 07:44 AM

All Answers

TomSnyderTomSnyder

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

 

 

 

 

 

 

Message Edited by tesii on 03-23-2010 07:44 AM
This was selected as the best answer
BshardiBshardi

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

BshardiBshardi

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?

 

 

Message Edited by Bshardi on 03-24-2010 08:48 AM
TomSnyderTomSnyder

 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>

BshardiBshardi

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?

 

TomSnyderTomSnyder

try:

<apex:repeat value="{!RelatedTo.Account.piccpqbms__Payments__r}" var="pay" > <apex:outputField value="{!pay.piccpqbms__paymentAmount__c}"/> </apex:repeat>

 

BshardiBshardi

Well, I tried the new code and now I'm getting another error but I'm not really sure what this one means:

 

ErrorError: ; nested exception is: common.exception.ApiQueryException: account.billingstate, account.shippingstate, account.piccpqbms__payments__r.piccpqbms__paymentamount__c ^ ERROR at Row:1:Column:162 Didn't understand relationship 'piccpqbms__payments__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

 

 

I used the following code:

 

 

<apex:repeat value="{!RelatedTo.Account.piccpqbms__Payments__r}" var="pay" > <apex:outputField value="{!pay.piccpqbms__paymentAmount__c}"/> </apex:repeat>

 

 

 

TomSnyderTomSnyder

execute this command from system log (link on top of page) to show/verify your relationshipname.

 

system.debug( Account.SObjectType.getDescribe().getChildRelationships());

 

BshardiBshardi

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. 

 

 

DHO_604DHO_604

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()); 

 

tmanutmanu
Hi Tesii,
                    Your answer is only working for lookup field and not to md field.if u have another option please share in this page.
Thanks