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
superlousuperlou 

Product2 invalid while Iterating over OpportunityLineItems

With the standard controller for Opportunity, I am trying to generate a list of line items (products) with custom formatting.  I am using the following:

 

    <apex:page renderAs="pdf" standardController="Opportunity">

    <h2>Items</h2>
    <apex:repeat value="{!Opportunity.OpportunityLineItems}" var="OLI">
    {!OLI.Product2.name}
    </apex:repeat>
    </apex:page>

 

However, Product2 seems to pretty clearly a standard field in the line item SObject.  Other fields like CreatedBy and Discount work fine.  Is there a gotcha for dealing with references?

 

Thanks,

Louis

akzeakze

Try this:

 

{!OLI.PricebookEntry.Product2.name}

 

instead of {!OLI.Product2.name}

 

 

Make sure to include the PricebookEntry.Product2.name in your SOQL statement.

 

~NK

sfdcfoxsfdcfox
As an aside (akze is correct here), I'd just like to add that Product2 is only valid for API version 3.0 or less (which is impossible to use in Apex Code). This is outlined in the Web Services API Developers Guide under the Standard Object reference. It pays to check out this document whenever you encounter a field that you think should exist but doesn't. All standard fields are included in this reference text.
superlousuperlou

Akze and sfdcfox, thanks for the clues!  That did the trick.

 

I'm not sure the most tactful way to put this, but is it common for gotchas like this to be in the Salesforce UI?  Under App Setup > Customize > Opportunities > Opportunity Products > Fields, there's no indication of PriceBookEntry and Product2 is clearly listed.  I'll treat the SOAP API guide (http://www.salesforce.com/us/developer/docs/api/index.htm) as the gold standard, but what causes this sort of discrepency (if it even is)?