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
Debra Shoup 4Debra Shoup 4 

Show Opportunity Products on Contracts Page with VisualForce

Hi,
I’m hoping someone can help me with a Visualforce page I’m trying to create. The requirement is to show the opportunity line items on the contract page. In our system, the opportunity is created, products are added and then a contract record is created from the opportunity.

The users don’t need to be able to edit the products, just view the product code and product name on the contract page. I’m new to developing and working my way through Visualforce and Apex on Trailhead, but I need to get this request complete pretty quickly.

Below is the code I have so far, which I've pieced together from various similar posts. However, it’s producing an error: List controllers are not supported for OpportunityLineItem. Any help would be greatly appreciated. Thank you so much!


My Visualforce Page
<apex:page >StandardController="Contract" extensions="ContractProducts">
  <apex:form >
        <apex:pageBlock title="Products">
            <apex:pageBlockTable value="{!lineItems}" var="oli">
                    <apex:column value="{!oli.ProductCode}"/>
                <apex:column value="{!oli.PricebookEntry.Name}"/>
             </apex:pageBlockTable>   
        </apex:pageBlock>
    </apex:form>
</apex:page>

My Apex Class
public class ContractProductsController {
 public List<OpportunityLineItem> lineItems { get; set; }
   Public ContractProductsController( ApexPages.StandardController std )
                {
                     if( std.getRecord().Id != null )
                                {
            Contract con = [ Select Id, Opportunity__c from Contract where Id =: std.getRecord().Id ];
                                               
    lineItems = [ Select Id, Opportunityid, PricebookEntry.Name, ProductCode
            from OpportunityLineItem where Opportunityid =: con.Opportunity__c ];
                                }
                                else
                                {
              lineItems = new List<OpportunityLineItem>();
                                }
    }        
}
 
Debra Shoup 4Debra Shoup 4
I found out from someone on the Trailblazer community that my error is in my opening statement. 

<apex:page StandardController="Contract" extensions="ContractProducts">

This made the VisualForce page available to select on the page layout. Now I have to figure out how to get the products to show up. What I have now produces an error "Invalid field ProductCode for SObject OpportunityLineItem. I changed it to the below, but all I get is the literal text as shown in the image.

<apex:page StandardController="Contract" extensions="ContractProductsController">
  <apex:form >
        <apex:pageBlock title="Products">
            <apex:pageBlockTable value="{!lineItems}" var="oli">
                <apex:column>
                  <apex:facet name="header">Product Code</apex:facet>
                       {ProductCode}
                </apex:column> 
                <apex:column>
                    <apex:facet name="header">Product</apex:facet>
                       {PricebookEntry.Name}
                </apex:column> 
              </apex:pageBlockTable>    
        </apex:pageBlock>
    </apex:form>
</apex:page>


 User-added image