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
StrangeRoutinesStrangeRoutines 

Accessing custom object parent fields in a standard controller

I have a custom object Quote__c with a lookup field called Enquiry__c to another custom object Enquiry__c. Eveyrthing I have read implies that in a standard controller for the child Quote__c object, I should be able to access fields in the parent object using dot notation, e.g., I should be able use the following line in an VFpage:

 

<p>Name is: {!Quote__c.Enquiry__c.Name}</p>

 

but when I try to save, I get the error Unknown property 'String.Name'

 

If I put it in an apex field

<p>Name is:<apex:outputField value="{!Quote__c.Enquiry__c.Name}"/></p>

 

I get the error "Could not resolve the entity from <apex:outputField> value binding '{!Quote__c.Enquiry__c.Name}'. outputField can only be used with SObject fields."

 

The documentation is REALLY confusing. In the Visual Force Developers Guide, in the section "Displaying Field Values with VisualForce", it says "You cannot access parent objects using this expression language. In other words, {!account.parent.name} will return an error." Yet, I tried that exact expression (on the implied standard objects) and it worked fine!

 

Then, in a later section "Accessing Data with a Standard Controller", it contradicts the previous statement by saying "You can traverse up to five levels of child-to-parent relationships. For example, if using the Contact standard controller, you can use {!contact.Account.Owner.FirstName} (a three-level child-to-parent relationship)" I tried that expression and IT worked fine.

 

Yet, my custom object expression cannot even access data in the first parent!  Are custom objects treated differently than standard objects by standard controllers?  Any hint as to what is going on would be deeply appreciated!

Ispita_NavatarIspita_Navatar

Let me understand your problem are you running a query in the controller and then trying to access the related parenjkt field?

Let me explain with an example:-

 

Say in the controller you have a method:-

 


Public List<Custom_Obj__c> getCustomObjs()
{
list<
custom_obj__c> Arr_custom_obj=[select id ,Name,Enquiry__c.Name from custom_obj__c ];
return Arr_custom_obj;
}

 

Then in VF page the following will work fine:-

 

<apex:pageBlock >
 <apex:pageBlockTable id="ptbl" value="{!CustomObjs}" var="t">
 <apex:column headerValue="Object Id">
  {!t.Id}
 </apex:column>
 <apex:column headerValue="Name">
 {!t.Name}
  </apex:column>
 <apex:column headerValue="Enquiry Name">
 {!t.Enquiry__c.Name}   (This will work fine with dot operator are you had queried it)
 </apex:column>
</apex:pageBlockTable>

 

But in case you want to access the related parent field as merge fields ( ex.g {!Quote__c.Enquiry__c.Name}) that will not be possible for custom fields and objects, I think it works for few standard objects and fields.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

 

 

StrangeRoutinesStrangeRoutines

Actually, my core problem is trying to understand inconsistent, conflicting documentation. At a higher level, if using dotted notation is supposed to let me access the parent of a child up through five levels, then I would like to do that because it should save time and code (and isn't that what SFDC is supposed to be about?)

 

In neither case in the doc, which is the VisualForce Devleoper's Guide, are they discussing Apex. They are dealing purely with VisualForce pages. In section "Displaying Field Values with Visualforce" they have a sample VFPage that uses the standard Accounts controller and the directive:

 

<p>You are viewing the {!account.name} account<p>

 

They then go on to say that "You cannot access parent objects using this expression language. In other words, {!account.parent.name} will return an error."

 

I tried that exact syntax,  {!account.parent.name}, directly in a VFPage, it is works fine.  So there is the first conflict in the doc.

 

Then in section "Accessing Data with a Standard Controller", it says, "For example, a page that uses the Account standard controller can use {!account.name} to return the value of the name field on the account that is currently in context."  So here again, we are talking about the VFPage, NOT a controller.  It then goes on to say, "You can traverse up to five levels of child-to-parent relationships. For example, if using the Contact standard controller, you can use {!contact.Account.Owner.FirstName} (a three-level child-to-parent relationship)"

 

I tried that syntax too, straight in the VFPage, and it worked fine too.  So here we have the second conflict, one page says you cannot traverse child-to-parent relationships and another says you can.

 

Finally, I tried the same traversal syntax with custom objects and it does NOT work.

 

So, I am hoping that I am either doing something wrong or somebody can confirm that the document contradicts itself and that  traversing child-to-parent with dotted notation does not work for custom objects, despite the fact that the document implies, by omission, that you can!

RailCIORailCIO
I am having the same  issue trying to read from a master to a junction detail object and then back to the second master for a VisualForce Email template.
prati@salesforceprati@salesforce
Hi
I am having the same issue. Did you  find a solution to that?
Thank you
Max Alexander 19Max Alexander 19
you need to do a querry , it will nto work directly using the notation, only the object fields
i.e. this.pageOpp = (Creative_Brief__c)stdController.getRecord();  thsi will notallow you to use
this pageOpp.Opportunity__r.Name
so you need to set up querry for it
LIst<Opportunity> oppylist =[select id,name from Opportunity where id  =:pageOpp.Opportunity__c];