+ Start a Discussion
Roc SuoRoc Suo 

Invalid field for SObject in a Visualforce page

Hi
 
I am trying to build a table of data in a page to show the list of policies details from the Account. Policy__c is the Custom Object.
However, it always show "Invalid field Policy__c for SObject Account.

I did try to modify <apex:pageBlockTable value="{! Account.Policy__c}" var="Policy"> to <apex:pageBlockTable value="{! Account.Policy__r}" var="Policy">, it doesn't work. the error message became  "Invalid field Policy__r for SObject Account.

How can I access the Custom Object from Standard Object? Thanks.
 .
 
<apex:page sidebar="false" standardController="Account">
    
    <apex:pageBlock title="Policy Detials">

   	<apex:pageBlockTable value="{! Account.Policy__c}" var="Policy">
     		 <apex:column value="{! Policy.Insurance_Company__c}"/>
      		<apex:column value="{! Policy.Name}"/>
                <apex:column value="{! Policy.Commencement_Date__c}"/>
 	</apex:pageBlockTable>

    </apex:pageBlock>
</apex:page>

 
Best Answer chosen by Roc Suo
bob_buzzardbob_buzzard
The '__r' notation will be the correct one, as there will be multiple policies on a single account. I suspect that its simply the relationship name that is incorrect. If you navigate to Setup -> Create -> Objects and open the Policy custom object, then open the relationship field to account, under lookup options you can see the relationship name against the 'Child Relationship Name' heading - here' an example from one of my dev orgs:

User-added image

Append __r to that name and you should be good (my guess is it's called Policies__r)

All Answers

bob_buzzardbob_buzzard
The '__r' notation will be the correct one, as there will be multiple policies on a single account. I suspect that its simply the relationship name that is incorrect. If you navigate to Setup -> Create -> Objects and open the Policy custom object, then open the relationship field to account, under lookup options you can see the relationship name against the 'Child Relationship Name' heading - here' an example from one of my dev orgs:

User-added image

Append __r to that name and you should be good (my guess is it's called Policies__r)
This was selected as the best answer
Kiranpreet KaurKiranpreet Kaur
Try this.

<apex:page sidebar="false" standardController="Account">
<apex:pageBlock title="Policy Detials">
<apex:pageBlockTable value="{! Account}" var="Policy">
<apex:column value="{! Policy__r.Insurance_Company__c}"/>
<apex:column value="{! Policy__r.Name}"/>
<apex:column value="{! Policy__r.Commencement_Date__c}"/>
</apex:pageBlockTable>
</apex:pageBlock> </apex:page>

Actually the pageblocktable contains the object not the field.

Please mark the answer if it works.
Abhilash Mishra 13Abhilash Mishra 13
Hi Roc Suo,

Please use child Realtionship name with __r in this case As you are Trying to reference one to many Relationship of Acount to Policy__c object.
 What I can realise you have lookup( or master deatil)  of account object in policy__C object. in that case to refer all the policies of a single account you have to Use child relationship name with appended __r.  its same like we user account.contacts to reference all the contact of a single account.

Let me Know if you need more help. mark it as solved by selecting it best answer. if it fixes the issue
Abhilash Mishra 13Abhilash Mishra 13
you can find child replationship name, when you click on Account lookup field in Policy__c object.
https://qas-shared-assets.s3.amazonaws.com/snaps/tws891g1igytlnm
Roc SuoRoc Suo
Problem sorted. Thanks for the solutions. When I checked the Account lookup field as your recommend, found the Child Relationship Name is "Policies" rather than "Policy", the Object API Name.

When I changed to  <apex:pageBlockTable value="{! Account.Policies__r}" var="Policy">, problem sort. 

What a stupid mistake I have!  Thanks for your help. 
Abhilash Mishra 13Abhilash Mishra 13
Hi Roc,
At some point, we all did this same mistake. :)
thing to remember is for one to many  relationship use child relationship Name with __r and for one to one relationship use object name with  __r notation.

Thanks :)
 
Jasmeen Kaur 15Jasmeen Kaur 15
Hi @Abhilash
I am experiencing a kind of similar problem and tried following the above mentioned steps as well but no luck.
Here is the case:
In my org there are 2 custom objects; 1. Potential Match 2. Incoming Associate Record.
There is a lookup relationship between these two. 
Now I am trying to create a VF page on Potential Match so that I can bring "Qualified to Lead" ( a field on Incoming Associate Record) on Potential Match Lightning Page Layout.
I tried writing apex class as below and its showing this error.
User-added image

Below is the relationship details of potential match and incoming associate record.
User-added image

Any help will be highly regarded.
Thanks in advance