[SalesForce] Call Child Object Fields Using Visualforce for Master-Detail Relationship

I am attempting to create a visualforce table section on the Account object. The Card Spend object is a child via master-detail to the Account object.

I keep getting "Error: Invalid field Card_Spend__c for SObject Account".

Why can I not reference the child object and its fields?

<apex:page standardController="Account">
    <apex:pageBlock title="Card Spend title">
    </apex:pageBlock>
    <apex:pageBlock title="Card Spend">
        <apex:pageBlockTable value="{!Account.Card_Spend__c}" var="Card Spend">
            <apex:column value="{!Card_Spend__c.Company_Credit_Limit__c}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

As always thank you for your help gentlemen.

Best Answer

If the Card_Spend__c is a child object then you need to refer by its child relationship name which you can find by opening detail page of field which you have created as a master detail on Card Spend object.

Please check snapshot to get idea about where you can find child relationship name. Make sure that you append __r after that name whenever you want to use it in apex/visualforce. enter image description here

Then your final code would be as below :

<apex:page standardController="Account">
    <apex:pageBlock title="Card Spend title">
    </apex:pageBlock>
    <apex:pageBlock title="Card Spend">
        <apex:pageBlockTable value="{!Account.Card_Spends__r}" var="cardSpend">
            <apex:column value="{!cardSpend.Company_Credit_Limit__c}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
Related Topic