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
ArjunDharArjunDhar 

Show related Recrods List (By SOQL) in a Subsections

Hi,

 am sure this is a regular question but I beat myself to find it on the forums. The Documents alsways state the sissy  Standard & Custom "Accounts" example.

 

Context:

So here goes, I have the following entity relation:

(Parent, many)  Shop

  |____ (Child , many) Goods

 

Its a many to many relation. From a Shop you can naturally see all the goods but not the other way round. So I wrote a SOQL to do that. Works.

 

Problem Statement:

However, am struggling with writing the Controller to show me a List of "Goods" (based on a Page query Param called Category), and for each Good there is a <apex:pageBlock > that should LIST the Shops that have that Good.

 

I addition to the solution in concept, I'd be greatful  if the following questions are also answered:

 

Places where I have difficulty understanding:

Q1) Can we get away with simply writing NO controller and using the "Goods" Standard Object as my Standard Contrroller? (I doubt, as I dont see Apex providing that much flexibility in scripts)

 

Q2) When I write a Standard Controller with Extensions; I try to write a method (see getShops) like:

   private final GOODS__r good;
   
   .... //Rest of controller code ...

   public ApexPages.StandardSetController goodsRecords{
	...
   }
    
    /**
     Get Shops for current Good
    **/
    public List<SHOP__c> getShops() {
        String goodsId =  this.good.id;
        return [SELECT .....
                FROM SHOP__c WHERE GOODS__r.id = :goodsId];

        //Problem: this.good.id is not pointing to anything, means the approach is bogus! :( Whats the point of a Class that has a member variable and also 
	//thinks its gets Lists via ApexPages.StandardSetController?!?
    }

 

Q3) If I write Custom methods, Apex code does not allow me to pass params. So if I write a method like:

getShops(goodId); I cant use that because I cant pass the parent record Id to my function in Apex and get the list either.

Or is there a way ???

 

Like this does not work: (where someId maybe derived from the parent list of Goods)

               <apex:dataTable id="dataTable" value="{!getShops('someId')}" var="subRec">
                    <apex:column value="{!subRec.Name}" />
                </apex:dataTable>

 

But if I had a methodd "getShops()" then using "shops" in Apex code would bind it to the getter. But as showin in my controller getShops() itself does not work

 

 

 

bob_buzzardbob_buzzard

Is the relationship between Good__c and Shop__c a lookup/master detail, or is there another object in between?  Or do they have lookups to each other?  

ArjunDharArjunDhar

Its a Look-master relation. So the Master will never have a relation to its lookups right; so through a standard object you cannot get to the Lookups. One has to SOQL. (Good_c is Master table).

bob_buzzardbob_buzzard

That's not a multi-multi relationship though - doesn't that mean a shop can only have a relationship to a single good (via the master/detail relationship to Good__c)?

ArjunDharArjunDhar

Ya i messed that Schematic representation of many to many. I apologize with that.

So I'm still playing with the object model ... I think the problem area of focus isnt really the Object relation but assuming I do wanna write SOQL (because there are other things that are needed, criterias etc in the query anyway)

 

So Assuming the scenario: (Main List = Goods, Sub List = Shops where that Good can be found)

- Cap

    - WallMart

    - GAP

- Tea

   - Stop n Shop

   - WallMart

- Bottle

   ...

   ...

- T-Shirt

   ...

   ...

 

So the top List can be generted in an <apex:dataTable> or something, now for each row I wanna query Shops for it. How does one do that?

 

I also understand its bad practice to call SOQL queries in Loops, but lets just ignore that and we want to achieve this using a Controller and Apex, how can we do it?

bob_buzzardbob_buzzard

Okay (if you didn't want SOQL I think you could do this without controllers).  However, if you go this route you shouldn't need to embed your query in a loop.

 

There's a couple of ways to approach this.  One mechanism is to retrieve all the parent good and child shop records in one query and iterate the parent and children:

 

public List<Good__c> goods {get; set;}

public MyController()
{
goods=[SELECT id, Name, (SELECT Shop__r.id, Shop__r.Name FROM Shops__r) FROM Good__c];

 page:

 

<apex:repeat value="{!goods}" var="good">
  <apex:outputText value="Good={!good.Name}"/><br/>
  <apex:repeat value="{!good.Shops__r}" var="shop">
     <apex:outputText value="Shop={!shops.Name}"/><br/>
  </apex:repeat>
</apex:repeat>

 

If you need a more complex structure, I wrote a blog post about grouping parents and children at:

 

http://bobbuzzard.blogspot.com/2011/06/grouping-records-and-children-in.html

 

that should give you a few hints.

ArjunDharArjunDhar
[SELECT id, Name, (SELECT Shop__r.id, Shop__r.Name FROM Shops__r) FROM Good__c]

 ...that is cool, didnt know one can write it like that.

 

Thanks, I'll validate the solution and confirm it back on this post. Was also thinking of fetching SubLists Using AJAX but thats another thing. For now thanks a lot.