2

I have a Custom Object that has a reference to Opportunities, I originally had it set up with a Master-Detail relationship but then after reading this and noting that parent objects can't be referenced, as well as the Master-Detail relationship only storing the ID, I switched the field type to a LookUp one.

So now my object's custom field's datatype is: Lookup(Opportunity) and I'm trying to make a VisualForce page that uses the StandardController (with reference to my custom object) render the name of the Opportunity. But the relationship doesn't seem to work and I just get unknown property String.name when I try. Can someone point me in the right direction?

Here's a bit of sample code:

<apex:page standardController="CustomObj__c" recordSetVar="objs">
    <apex:pageBlock title="">
         <apex:pageBlockTable value="{! objs}" var="item" >
            <apex:column headerValue="Opp. Name">
                <apex:outputText value="{! item.opp_to_exp__c.Name}"/>
            </apex:column>
            <apex:column headerValue="Opportunity">
                <apex:outputText value="{! item.opp_to_exp__c}"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

So opp_to_exp__c is API name for the field, and I've ripped out the specifics of the names and such besides that API name. I have no trouble rendering the other fields up of the object, just this lookUp one it seems.

The error message suggests to me that the opp_to_exp__c is still just a String for some reason even though I've changed the data type to be a LookUp one.

Can anyone help? I've spent over a few hours reading documentation and trying to find the right KeyTerms to search for on here with no avail.

2 Answers 2

2

Did you try replacing __c with __r i.e.

item.opp_to_exp__r.Name

That's typically how you reference related objects

1
  • Oh jeez. Nope, but that just worked. I must have managed to stare right past it in the docs. Thanks! Jan 21, 2014 at 20:56
1

I think you're confusing some stuff. What do you mean by "parent objects can't be referenced"? Or by "Master-Detail relationship only storing the ID" (because so does the lookup relationship; let's say M-D has different benefits/considerations but in both cases only the Id - like a foreign key - is stored on the object that has the lookup field).

To quick & dirty fix your Visualforce - use <apex:outputText value="{! item.opp_to_exp__r.Name}"/>.

To read a bit why - check http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_relationships.htm and http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_custom_objects.htm

4
  • I accidently linked to the wrong thing in my question, I was referring to this: salesforce.com/us/developer/docs/pages/… where it says: "You cannot access parent objects using this expression language. In other words, {!account.parent.name} will return an error." which was confusing to me Jan 21, 2014 at 21:04
  • Hm, wrong link again? :) salesforce.com/us/developer/docs/salesforce1api/Content/… I assume. I think it's incorrect, this works just fine for me: <apex:page standardController="Account"> <apex:outputText value="{!account.parent.name}" /> </apex:page> (need to access it as /apex/pageName?id=someAccountsId, preferably with Id of an account that has a parent but not necessarily. You can even go {!account.parent.owner.profile.name} if you want.
    – eyescream
    Jan 21, 2014 at 21:21
  • Gah, I don't know why it keeps using the wrong link, I swear I put in the right one! But yes the one you linked to there is the I was referring to. Thanks. Jan 21, 2014 at 21:26
  • Docs use iframes, very annoying indeed. I either right click & open frame in new tab (FF) or click the top "email me this" icon and copy from there...
    – eyescream
    Jan 21, 2014 at 21:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .