+ Start a Discussion
Gregory512Gregory512 

Displaying a list of products in a visualforce page

Hi everyone,

 

I want to generate receipts from the opportunity object.  The way I think this should work is a custom button or link that links to a visualforce page.  On the VF page I want to list all products related to that opportunity. 

 

Here's some code that I think points me in the right direction, but I'm not sure exactly where to go next.  It's from the Force.com Cookbook.. directions how to show a table of contacts related to an account.  This is the controller code:

 

public class mySecondController { public Account getAccount() { return [select id, name, (select id, firstname, lastname from Contacts limit 5) from Account where id = :System.currentPageReference() .getParameters().get('id')]; } public String getName() { return 'My Second Custom Controller'; } }

 

And here's the sample VF page code to display the data:

 

 

<apex:page controller="mySecondController" tabStyle="Account"> <apex:pageBlock title="Hello {!$User.FirstName}!"> You belong to the {!account.name} account. </apex:pageBlock> <apex:pageBlock title="Contacts"> <apex:dataTable value="{!account.Contacts}" var="contact" cellPadding="4" border="1"> <apex:column > {!contact.FirstName} </apex:column> <apex:column > {!contact.LastName} </apex:column> </apex:dataTable> </apex:pageBlock> </apex:page>

Thanks for any help or suggestions.

Best Answer chosen by Admin (Salesforce Developers) 
prageethprageeth

Hi Greg;

There is no field named "Name" in OpportunityLineItem table. So you can't write as "op.name".

 You can't refer as "{!OP.Product2}".

If you want the name of the product you need do write something as below.

 

{!op.priceBookEntry.product2.name}

 


 

All Answers

Gregory512Gregory512

So I found a much simpler solution to displaying related list information on a visualforce page, but I can't access product information.

 

It should look something like this:

 

<apex:pageblockTable value="{!Opportunity.Product__r}" var="OP"> <apex:column value="{!OP.Name}"> </apex:column> <apex:column value="{!OP.Quantity}"> </apex:column> </apex:pageblockTable>

 

It's not recognizing products as a related object.  What's so different about the relationship between products and opportunities that this can't be done?

prageethprageeth

Hello Gregory;

If you need to get the OpportunityLineItems(products) under an Opportunity change the value of the table as below.

 

<apex:pageblockTable value="{!opportunity.opportunityLineItems}" var="op">

 I the "product" is a Custom Object make sure to use the childRelationshipName at the palce of "opportunityLineItems".

 

Gregory512Gregory512

Thanks for your help.   That got me to the next step, but I ran into another issue.

 

I can pull in the values for quantity and price of products, but not the product name:

 

 

<apex:pageblockTable value="{!Opportunity.opportunityLineItems}" var="OP">
<apex:column value="{!OP.Product2}">
</apex:column>
<apex:column value="{!OP.Quantity}">
</apex:column>
<apex:column value="{!OP.TotalPrice}">
</apex:column>
</apex:pageBlockTable>

 I have the same problem with product code.  If I look at Opportunity Product fields in setup, Product and Product Code have a field on their detail page with the label  "Child Relationship Name" and value "OpportunityLineItems."  Quanity and price don't have this field.   So basically {!OP.Product2} isn't correctly referencing the product.. I tried a number of other things, but can't figure it out.  Thanks for your help.

 

 

prageethprageeth

Hi Greg;

There is no field named "Name" in OpportunityLineItem table. So you can't write as "op.name".

 You can't refer as "{!OP.Product2}".

If you want the name of the product you need do write something as below.

 

{!op.priceBookEntry.product2.name}

 


 

This was selected as the best answer