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
M MillerM Miller 

Showing Products on a Visualforce page

I am working on a visualforce page with the Account standard controller, and am pulling in fields from the Account, Contact, and Opportunity objects with no problem. My problem is that I also need to show the associated Products, but I can't for the life of me figure out how to do this. The way I brought in contact and opportunity fields associated with an account (apex:pageblocktable value="{!Account.contacts}"  var="contact") isn't working for associated products. When I set up (apex:pageblocktable value="{!Account.products}"  var="product") or any variation like productitems or opportunityproducts, I get an error message of "Invalid field for sObject Account".

I'm really new to Visualforce, and am struggling to understand why this is such a problem! Is there any way to get the name of a product on my VF page?
Murali Guntakal 5Murali Guntakal 5
I guess there is no direct relation between your Products and Account. 
Murali Guntakal 5Murali Guntakal 5
You can write a extension to display Products related to your all Opportunities that has relationship with the account.

 
M MillerM Miller
Actually, I was able to solve this by taking advantage of an existing related list that shows Products on the Account record. For anyone else who might find this helpful, I was able to find the fields for this custom related list under Setup/Create/Objects. From there, I was able to bring in the fields I needed with the following code:
<apex:pageblocktable value="{!Account.Product_Items__r}" var="product_items__r" columns="2">
            <apex:column headervalue="Product Item(s)">
                <apex:outputfield value="{!Product_Items__r.Name}"> </apex:outputfield> </apex:column>
            <apex:column headervalue="Product Name(s)">
                <apex:outputField value="{!Product_Items__r.Product__c}"> </apex:outputfield> </apex:column>
        </apex:pageblocktable>

For other beginners like me, who also benefit from having the obvious stated:
Note that the __r specifies that it's a related list, and the __c specifies that it's a custom field from that related list.

This is also a great way to only bring in certain fields from a related list - I was originally putting in <apex:relatedlist list="product_items"/>, but that copied over EVERYTHING from the related list view, including the edit and delete links, fields I didn't want, and the buttons along the top. Not the greatest view for a static report.