Can any one help me out on this Error: Invalid field selected for SObject OpportunityLineItem. This is a page created for multiple row to edit.Any suggestion plz. Here is the VF page :

<apex:form id="theForm">
       <!--saving editing value -->
       <apex:commandButton value="Save" action="{!tosave}" id="saveButton"  />

       <apex:outputPanel style="float:middle">

      <!--display pageblock table.....-->

          <apex:pageBlockTable value="{!OpportunityLineItem}" var="o" cellpadding="2" border="1"  rowClasses="odd,even" styleClass="tableClass" id="opp_table">
              <apex:column headerValue="All OpportunityProduct Name" >

                 <!--displaying checkbox inside column...-->

                 <apex:inputcheckbox value="{!o.selected}">

              <!--invoking a controller function to know selected contacts.....-->

                   <apex:actionSupport event="onclick" action="{!getSelected}" rerender="Selected_PBS"/>
                <b><apex:outputField value="{!o.opp.product2.productcode}"/> </b>    


             <apex:column headerValue="QUANTITY" > 
                <apex:outputfield value="{!o.opp.Quantity}" /> 

             <apex:column headerValue="SALES PRICE"> 
               <apex:outputField value="{!o.opp.Unitprice}"/>

            <apex:inlineEditSupport event="ondblClick"  showOnEdit="saveButton"/>




Here is the code

public class MultipleOppRowEditing {

     //All property....

     public OpportunityLineItem oli{get;set;}
     public Id Id{get;set;}
     List<oliwrapper> oliList = new List<oliwrapper>();
     List<OpportunityLineItem> selectedOpportunityLineItem = new List<OpportunityLineItem>();


     public MultipleOppRowEditing(ApexPages.StandardController controller) {

        OpportunityLineItem oli = new OpportunityLineItem();


     //displaying contact pageBlock table....

     public  List<oliwrapper> getoli() {

        for(OpportunityLineItem o: [select Id, product2.productcode,Unitprice,Quantity from OpportunityLineItem order by createdDate desc ])

              oliList.add(new oliwrapper(o));
              return oliList;

       //all selected contacts...for edit...       
     public PageReference getSelected() {

         for(oliwrapper oppwrapper: oliList) 
            if(oppwrapper.selected == true)


            return null;

     public List<OpportunityLineItem> GetSelectedoli(){

        return selectedOpportunityLineItem;
       // ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select at least one opportunity.'));
        return null;

    //saving edited value...

      public PageReference tosave() { 

        update selectedOpportunityLineItem;
        PageReference MultiOppLineItemEdit=new PageReference('/apex/MultiOppLineItemEdit');
        return MultiOppLineItemEdit;
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select at least one OppLineItem.'));
         return null;


    //wrapper class...

    public class oliwrapper
        public OpportunityLineItem opp{get; set;}
        public Boolean selected {get; set;}
        public oliwrapper(OpportunityLineItem o)
            opp = o;
            selected = false;
  • @Folkert:Thanks for you reply.I tried replacing as ProductCode ,but it still throws the same error.
    – nikkey
    Nov 21, 2014 at 11:29
  • Does it give a line number? Could you point us to which line of code throws the error? And have you also replace ProductCode in the VF page? {!o.opp.product2.productcode}
    – Folkert
    Nov 21, 2014 at 11:34
  • @Folkert:It does not give any line number ,and the error is reflected on the vf page ,the code got saved in the apex class.PFA Screen SHot
    – nikkey
    Nov 21, 2014 at 11:51
  • Try putting a debug log on your user and see if it gives a line in those logs.
    – Folkert
    Nov 25, 2014 at 13:29
  • you will have endless debugging problems if in your oliWrapper inner class you use a variable named opp to refer to an OpportunityLineItem. opp conveys notion of an opportunity, not a line item. The constructor in this inner class is equally afflicted
    – cropredy
    Jan 21, 2015 at 20:36

3 Answers 3


Reason you are getting this is error is because you are querying relationship Product2 that does not exist on OpportunityLineItem. The way product is linked to opp line item is via PriceBookEntry object.

Your query should look like this:

[SELECT Id, PricebookEntry.ProductCode, Unitprice, Quantity 
 FROM OpportunityLineItem 
 ORDER BY CreatedDate DESC ]

I think you try to select a field that doesn't exist in this query:

[select Id, product2.productcode,Unitprice,Quantity from OpportunityLineItem order by createdDate desc ]

Can you try ProductCode instead of product2.productcode?

See the Object reference: http://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_opportunitylineitem.htm


Looks like you have a lower case c on CreatedDate in your order by clause.

[select Id, product2.productcode,Unitprice,Quantity from OpportunityLineItem order by **c**reatedDate desc ]

Should be

[select Id, product2.productcode,Unitprice,Quantity from OpportunityLineItem order by **C**reatedDate desc ]

Also I think you need to include CreatedDate in your list fields to pull.

 [select Id, product2.productcode,Unitprice,Quantity,CreatedDate from OpportunityLineItem order by **C**reatedDate desc ]
  • welcome to SE. while posting any code you can format the code from edit toolbar. it will help to identify the code from rest of the text.
    – Himanshu
    Feb 11, 2016 at 19:00

You must log in to answer this question.

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