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
Parikhit SarkarParikhit Sarkar 

How can a developer display data from the related Supplier__c records on a Visualforce page that has a standard controller for the Buyer__c object?

An org has a data model with a Buyer__c object that has a lookup relationship to Region__c and a Supplier__c object has a lookup relationship to Region___c.
How can a developer display data from the related Supplier__c records on a Visualforce page that has a standard controller for the Buyer__c object?



A. Use rollup formula fields on the Buyer__c object to reference the related Supplier__c records through the Region__c.
B. Use SOQL in a controller extension to query for related Supplier__c records.
C. Use a second standard controller for the Region__c object on a page to display the related Supplier__c records.
D. Use merge field syntax to retrieve the Supplier__c records related to the Buyer__c record through the Region__c.


I believe the answer should be D but everywhere else it says the answer is A - I do not understand why though. Can anyone help affirming my belief? Thanks in adv.
Best Answer chosen by Parikhit Sarkar
Andrew GAndrew G
I will throw a cat amongst the pigeons.   I vote B.

Let assume that Region is the parent and there are many Buyers and many Suppliers in the Region, hence the lookup relationship.

A.  - Rollup formula go upwards - for example, the parent (region) will count the child records (buyers).  Therefore a rollup formula on the buyer is not possible in this schema.
C.  Use a second standard controller - no such animal exists - there is one Standard controller per object, or a Custom controller or an extension to a standard controller.
D.  Merge fields.  Merge fields cannot go up and down (in a single path) a relationship - they either go up or they go down.  You cannot go up from Buyer to Region and then back down to Supplier with a merge field.

By default winner is B.

But if we want to explain - yes, I can create a controller extension that adds functionality to the (one) standard controller and in the controller I can create a SOQL query that will find the Supplier records related to the Region that contains the Buyer record using the Region field.

Now, even we say that the relationship as described is that the Region is a child to both the Buyer and the Supplier object, then the reasons why or why not remain the same.
A.  Rollups count or sum children records - the path is up, not sideways.
C.  Second standard controller still does not exist.
D.  Merge field can on go in one direction at a time.

Winner B - for the same reasons.

Regards

Andrew

 

All Answers

Andrew GAndrew G
I will throw a cat amongst the pigeons.   I vote B.

Let assume that Region is the parent and there are many Buyers and many Suppliers in the Region, hence the lookup relationship.

A.  - Rollup formula go upwards - for example, the parent (region) will count the child records (buyers).  Therefore a rollup formula on the buyer is not possible in this schema.
C.  Use a second standard controller - no such animal exists - there is one Standard controller per object, or a Custom controller or an extension to a standard controller.
D.  Merge fields.  Merge fields cannot go up and down (in a single path) a relationship - they either go up or they go down.  You cannot go up from Buyer to Region and then back down to Supplier with a merge field.

By default winner is B.

But if we want to explain - yes, I can create a controller extension that adds functionality to the (one) standard controller and in the controller I can create a SOQL query that will find the Supplier records related to the Region that contains the Buyer record using the Region field.

Now, even we say that the relationship as described is that the Region is a child to both the Buyer and the Supplier object, then the reasons why or why not remain the same.
A.  Rollups count or sum children records - the path is up, not sideways.
C.  Second standard controller still does not exist.
D.  Merge field can on go in one direction at a time.

Winner B - for the same reasons.

Regards

Andrew

 
This was selected as the best answer
Parikhit SarkarParikhit Sarkar

Thanks a lot Andrew. You're absolutely spot on with the Answer, it is indeed B. To verify your reasoning I did a bit of R&D myself and recreated a similar data model in my Org - 

erd

I have also created a VF page with a custom controller for Buyer__c and a custom extension for querying related data from Supplier__c. 

BuyerPage.vfp - 

<apex:page controller="BuyersSamplePageController" extensions="BuyersSamplePageControllerExt" sidebar="false">    
    <style type="text/css">
        .outBorder {
        border:3px outset black;
        }
        .inBorder{
        border-top:2px dotted blue;
        border-left:2px dotted blue;
        }
        .inlineTable {
            display: inline-flex !important;
        }
    </style>
    
    <apex:pageBlock title="List of Buyer & Suppliers" >    
    <div class="inlineTable">
        <apex:dataTable value="{!buyersList}" var="b" styleclass="outBorder" width="550px" >
            <apex:column styleclass="inBorder">
                <apex:facet name="header">Buyer ID </apex:facet> 
                <apex:outputText >{!b.Id}</apex:outputText>
            </apex:column>
            <apex:column styleclass="inBorder">
                <apex:facet name="header">Buyer Name </apex:facet>
                <apex:outputText >{!b.Name}</apex:outputText>
            </apex:column>    
        </apex:dataTable>
        
            <!--  nesting suppliers table  (not recommended) -->
        <apex:dataTable value="{!supplierList}" var="s" styleclass="outBorder" width="550px">            
            <apex:column styleclass="inBorder">
                <apex:facet name="header">Supplier ID </apex:facet> 
                <apex:outputText >{!s.Id}</apex:outputText>
            </apex:column>            
            <apex:column styleclass="inBorder">
                <apex:facet name="header">Supplier Name </apex:facet>
                <apex:outputText >{!s.Name}</apex:outputText>
            </apex:column>                   
        </apex:dataTable>
    </div>      
    </apex:pageBlock> 
</apex:page>

BuyersSamplePageController - 
 
public class BuyersSamplePageController {    
    public List<Buyer__c> buyersList{get;set;}    
    public BuyersSamplePageController(){
        buyersList = new List<Buyer__c>();
        buyersList = [select id,name from Buyer__c];
    }    
}

BuyersSamplePageControllerExt - 
 
public class BuyersSamplePageControllerExt {    
    public List<Supplier__c> supplierList{get; set;}
    public BuyersSamplePageControllerExt(BuyersSamplePageController custCtrl){
        supplierList = new List<Supplier__c>();        
        supplierList = [SELECT id, name FROM Supplier__c] ;
    }
}


Buyers/Suppliers list - 

vf

As you can see here, I have been able to retrieve data from Supplier in VF page using extensions! 

Side note: I have nested the 2 tables for buyers & suppliers, for illustration purposes only. If you want to combine two different objects in a single table and display it in a VF page then use a Wrapper class. 

Regards, 

Parikhit.