Retrieving parent record from child record in Salesforce

Retrieving parent record from child record in Salesforce

Sample Code:

Visualforce Page:

<apex:page controller="sample" >
    <apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!intList}" var="i">
            <apex:column value="{!i.Name}"/>
            <apex:column value="{!i.Member__r.Name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Controller:

public class sample
{   
    public List<Interest__c> intList {get;set;}
    public sample()
    {
        String sql = 'SELECT Name, Member__r.Name FROM Interest__c';
        intList = Database.Query(sql);
    }   
}

Output:

Leave a Reply