+ Start a Discussion
Femila Ice CreamFemila Ice Cream 

Not able to display opportunitylineitem in opportunity controller

<apex:page standardcontroller="Opportunity" extensions="myOpptyController" tabStyle="Opportunity">

          <apex:pageBlock title="opportunity product related list">

             <apex:pageBlockTable value="{!opptyList}" var="div">

               <apex:column >

                        <apex:pageBlockTable value="{!div.OpportunityLineItems}" var="custom">

                        <apex:column value="{!custom.Quantity}"/>

                        <apex:column value="{!custom.UnitPrice}"/>

                        <apex:column value="{!custom.TotalPrice}"/>

                        <apex:column value="{!custom.PricebookEntry.Name}"/>

                        <apex:column value="{!custom.PricebookEntry.Product2.Family}"/>

                    </apex:pageBlockTable>

            </apex:column>   

     </apex:pageBlockTable>

    </apex:pageBlock>

    </apex:page>

Class is as follows:
public class myOpptyController {

    public myOpptyController(ApexPages.StandardController controller) {

    }     

        public List<Opportunity> opptyList{get;set;}

       // public List<OpportunityListItems> opptyList1{get;set;}

        public myOpptyController() {
     

        opptyList = [SELECT Id,Name,

             (SELECT ID, Quantity, UnitPrice, TotalPrice,PricebookEntry.Name, PricebookEntry.Product2.Family FROM OpportunityLineItems)

                            FROM Opportunity WHERE Id =: ApexPages.currentPage().getParameters().get('ID')];

                  //   System.debug('opptyList ='+opptyList);

        }

         

      //  public List<Opportunity> getopptyList() {

      //      return opptyList;
    //    }
      

    }
This code is not displaying opportunitylineitem table
Dileep KumarDileep Kumar

Hi Femila Ice Cream,

If you want to show the opportunity line item on page please follow below instruction,
public class myOpptyController {
 public List<OpportunityLineItem> oppLineItemList{get;set;}

 public myOpptyController(ApexPages.StandardController controller) {
   List<Opportunity>        opptyList = [SELECT Id,Name,

             (SELECT ID, Quantity, UnitPrice, TotalPrice,PricebookEntry.Name, PricebookEntry.Product2.Family FROM OpportunityLineItems)

                            FROM Opportunity WHERE Id =: ApexPages.currentPage().getParameters().get('ID')];
        for(Opportunity opp :opptyList){
              oppLineItemList = opp.OpportunityLineItems,
              system.debug('@@oppLineItemList@@'+oppLineItemList);
        }
    }   
}



<apex:page standardcontroller="Opportunity" extensions="myOpptyController" tabStyle="Opportunity">

          <apex:pageBlock title="opportunity product related list">

             <apex:pageBlockTable value="{!oppLineItemList}" var="div">

                        <apex:column value="{!div.Quantity}"/>

                        <apex:column value="{!div.UnitPrice}"/>

                        <apex:column value="{!div.TotalPrice}"/>

                        <apex:column value="{!div.PricebookEntry.Name}"/>

                        <apex:column value="{!div.PricebookEntry.Product2.Family}"/>

     </apex:pageBlockTable>

    </apex:pageBlock>

    </apex:page>

Please try it and let me know.

If it will helpful.please mark it as a best answer.

Thanks,

Dileep

Virendra ChouhanVirendra Chouhan
Hi Femila Ice Cream,

You have used apex class as an extension in your vf page so need to use parameterized constructor to initialize values.

Use below controller code.
public class myOpptyController {

    public myOpptyController(ApexPages.StandardController controller) {

        opptyList = [SELECT Id,Name,

             (SELECT ID, Quantity, UnitPrice, TotalPrice,PricebookEntry.Name, PricebookEntry.Product2.Family FROM OpportunityLineItems)

                            FROM Opportunity WHERE Id =: ApexPages.currentPage().getParameters().get('ID')];

                  //   System.debug('opptyList ='+opptyList);


    }     

        public List<Opportunity> opptyList{get;set;}

       // public List<OpportunityListItems> opptyList1{get;set;}

        public myOpptyController() {
     
        }

         

      //  public List<Opportunity> getopptyList() {

      //      return opptyList;
    //    }
      

    }

 
Jagadeesh111Jagadeesh111

Hello Femila Ice Cream,
 

Above mentioned 2 answers are correct. Please find my answer too.

Note : Create Opportunity Line Item to opportunity and provide id as shown in image.

Visualforce Page : 

<apex:page standardcontroller="DisplayOpportunityLineItem" extensions="myOpptyController" tabStyle="Opportunity">
    <apex:pageBlock title="opportunity product related list">
          <apex:pageBlockTable value="{!opptyList}" var="div">
               <apex:column >
                        <apex:pageBlockTable value="{!div.OpportunityLineItems}" var="custom">
                        <apex:column value="{!custom.Quantity}"/>
                        <apex:column value="{!custom.UnitPrice}"/>
                        <apex:column value="{!custom.TotalPrice}"/>
                        <apex:column value="{!custom.PricebookEntry.Name}"/>
                        <apex:column value="{!custom.PricebookEntry.Product2.Family}"/>
                    </apex:pageBlockTable>
            </apex:column>   
        </apex:pageBlockTable>
   </apex:pageBlock>
</apex:page>



Controller Class : 

public class DisplayOpportunityLineItemController {
   public List<Opportunity> opptyList{get; set;} //List to store opportunities and OpportunityLineItems
     
    public DisplayOpportunityLineItemController(ApexPages.StandardController controller) {
         opptyList = [SELECT Id,Name, (SELECT ID, Quantity, UnitPrice,  TotalPrice,PricebookEntry.Name, PricebookEntry.Product2.Family FROM OpportunityLineItems)
                      FROM Opportunity
                      WHERE Id =: ApexPages.currentPage().getParameters().get('Id')];
    }     
    
}

 

User-added image

 

Please select Best answer from above 3 answers. :)

 

Regards,
Jagadeesh