0

Any help would be greatly appreciated (either in actual code help or pointing me in the right direction).

I'm not much of a developer but was trying to get a related list from a custom object to display on another custom object. Basically I have a 'Bundled Products' object which has a products that are bundled together through a conjunction object ('Product_Bundle_Products_Association__c') connecting Bundled Products to Products. I want to display the related list of the conjunction object on another object which is 'Quote Bundles'. Right now I would be happy with just the Product Name but I am going to need more columns eventually.

The error that I am getting back on my page where I have dropped the visualforce at is 'Content cannot be displayed: SObject row was retrieved via SOQL without querying the requested field: Quote_Bundle__c.Product_Bundle__r'.

I'm thinking this should be a simple fix but for the life of me I can't figure out what to do. Here is my class:

public class QuoteBundleClass {  

private List<Product_Bundle_Products_Association__c> pbpa;

private Quote_Bundle__c qb;

public QuoteBundleClass(ApexPages.StandardController controller) {

    this.qb = (Quote_Bundle__c)controller.getRecord();

    }    

    public List<Product_Bundle_Products_Association__c> getpbpa()

    {
        Quote_Bundle__c qbl = [Select id, Name, Product_Bundle__c, Product_Bundle__r.Id
                              From Quote_Bundle__c
                              Where id = :qb.Id];        

    if(qbl.Product_Bundle__c == null)
    return null;        
    pbpa = [SELECT Id, Product__r.Name, Name
            FROM Product_Bundle_Products_Association__c
            WHERE Product__r.Id = :qb.Product_Bundle__r.Id];
                     return pbpa;
    }

}

And here is my visualforce page:

<apex:page standardController="Quote_Bundle__c" extensions="QuoteBundleClass">      
<apex:form >
    <apex:pageMessages />
    <apex:detail relatedList="true"></apex:detail>
    <apex:pageblock id="CustomList" title="Bundled Products"  >
        <apex:pageBlockTable value="{!pbpa}" var="p" rendered="{!NOT(ISNULL(pbpa))}">
            <apex:column>
                <apex:outputField value="{!p.Name}" />
            </apex:column>
        </apex:pageBlockTable>
        <apex:outputLabel value="No records to display" rendered="{!(ISNULL(pbpa))}" styleClass="noRowsHeader"></apex:outputLabel>
    </apex:pageblock>
</apex:form>        

Thanks in advance guys and gals!

2
  • as an aside, I'd avoid returning null for getters with a return type of List<..>; instead, return empty lists
    – cropredy
    Jan 22, 2016 at 23:48
  • FYI, any answer may involve the fact that we are trying to retrieve a product list. It seems like this is a part of the answer. Let me know any background or answer if possible.
    – wadams2010
    Jan 23, 2016 at 7:12

4 Answers 4

2

I tried this a few different ways and figured out I might not have been ensuring that the Product_Bundle__r.Id was the same as the Quote_Bundle__c.Product_Bundle__r.Id. I also had some issues with the Visualforce page using the related list and followed the Salesforce Visualforce Related List guide.

Below is my code. Let me know if you all have any questions.

public class QuoteBundleClass {  

private List<Product_Bundle_Products_Association__c> pbpa;

private Quote_Bundle__c qb;

public QuoteBundleClass(ApexPages.StandardController controller) {

    this.qb = (Quote_Bundle__c)controller.getRecord();
    }    

 public List<Product_Bundle_Products_Association__c> getpbpa() 

{
 Quote_Bundle__c qbl = [Select id, Name, Product_Bundle__r.Id
                        From Quote_Bundle__c
                        Where id = :qb.Id];        

if (qbl.Product_Bundle__c == null) {
    return null;        
}

pbpa = [SELECT Id, Name, Product_Bundle__r.Id
        FROM Product_Bundle_Products_Association__c
        WHERE Product_Bundle__r.Id = :qbl.Product_Bundle__r.id];
return pbpa;
}
}

And here is the visualforce

<apex:page standardController="Quote_Bundle__c" extensions="QuoteBundleClass">      
<apex:pageBlock title="Products">
    <apex:form>
        <apex:pageBlockTable value="{!pbpa}" var="p" rendered="{!NOT(ISNULL(pbpa))}">
            <apex:column >
                <apex:outputField value="{!p.Name}" />
            </apex:column>
        </apex:pageBlockTable>
    <apex:outputLabel value="No records to display" rendered="{!(ISNULL(pbpa))}" styleClass="noRowsHeader"></apex:outputLabel>
    </apex:form>
</apex:pageBlock>    

0

This is what I would do, rewrite your method with this:

public List<Product_Bundle_Products_Association__c> getpbpa() 
{
    if (qb.Product_Bundle__c == null) {
      return null;        
    }

    pbpa = [SELECT Id, Product__r.Name, Name
            FROM Product_Bundle_Products_Association__c
            WHERE Product__r.Id = :qb.Product_Bundle__c];

    return pbpa;
}

If that fails with a similar error then you can add the field Product_Bundle__c to the page layout and that should fix it. (Note: The field Product_Bundle__c is identical to Product_Bundle__r.Id, so there is no need to use both)

If it doesn't work, you have yet another thing to try (see below), but I think the first snippet alone will do it.

public List<Product_Bundle_Products_Association__c> getpbpa() 
{
     Quote_Bundle__c qbl = [Select id, Name, Product_Bundle__c
                              From Quote_Bundle__c
                              Where id = :qb.Id];        

    if (qbl.Product_Bundle__c == null) {
      return null;        
    }

    pbpa = [SELECT Id, Product__r.Name, Name
            FROM Product_Bundle_Products_Association__c
            WHERE Product__r.Id = :qb1.Product_Bundle__c];

    return pbpa;
}
0

Why use code to accomplish this? You can do this quite easily in a declarative way after creating a second junction object called Quote Bundle_Product_Association__c with MD relationships to Quote Bundle and Products. Here are some links to help out:

  1. Help File for Many-to-Many Relationships
  2. Walkthrough on creating Many-to-Many Relationships with Related List placement
2
  • 1
    You can't do a MD relationship with Products. I am only doing lookup relationships. Do you think this would still be accomplish able through this method?
    – wadams2010
    Jan 23, 2016 at 2:29
  • 2
    I didn't realize that when I posted. This won't work as advertised with the product table. Jan 23, 2016 at 2:42
0

If I understand your schema correctly, you have

Quote_Bundle__c with lookup to Product_Bundle__c
Product_Bundle__c is parent of Product_Bundle_Products_Association__c
Product_Bundle_Products_Association is child of Product2

So, the related list you need to return is given by:

public Product_Bundle_Products_Association__c[] getPbpa() {
   return this.qb.product_bundle__c == null
       ? new List<Product_Bundle_Products_Association__c> ()
       : [select id, 
            (select id, product__r.name, product_r.otherProductFldX__c, ...
                from Product_Bundle_Products_Associations__r)
           from Product_Bundle__c
           where id = :this.qb.product_bundle__c].product_bundle_Product_associations__r;
} 

and your VF page fragment is:

<apex:pageBlockTable value="{!pbpa}" var="p" rendered="{!pbpa.size>0}">
    <apex:column>
       <apex:outputField value="{!p.product__r.name}" />
    </apex:column>
 </apex:pageBlockTable>
 <apex:outputLabel value="No records to display" rendered="{!pbpa.size=0}" styleClass="noRowsHeader"></apex:outputLabel>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .