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
Rishiraj Singh 17Rishiraj Singh 17 

How do I find the count of products associated to an opportunity using the Apex code in Salesforce?

I am working for a client request to develop a Opportunity Summary Page(Visualforce page), for which I need to show below recordds for the number of opportunities:

Opportunity NameAccount NameProducts NameAmountQuantitySubTotalNumber of ProductsTotal Payable AmountStageApproval History
Best Answer chosen by Rishiraj Singh 17
sfdcMonkey.comsfdcMonkey.com
hi rishairaj ,
use below code :
 
<apex:page controller="OppExtension">
    <apex:form >
      <apex:pageBlock >
          <apex:pageblockSection title="Opportunities Summary Details" collapsible="true" Columns="10">
              <apex:pageBlockTable value="{!opportunityLst}" var="opp">
                  <apex:column value="{!opp.Name}"/>
                  <apex:column value="{!opp.Account.name}"/>
                  <apex:column headerValue="Product Details">
                  <apex:pageBlockTable value="{!opp.opportunitylineitems}" var="opplt">
                      <apex:column value="{!opplt.Product2.Name}"/>
                      <apex:column value="{!opplt.ListPrice}"/>
                      <apex:column value="{!opplt.Quantity}"/>
                      <apex:column value="{!opplt.TotalPrice}"/>
                  </apex:pageBlockTable>
                  </apex:column>
                  <apex:column headerValue="Number Of Products">
                      <apex:outputText value="{!opp.opportunitylineitems.size}"></apex:outputText>
                  </apex:column>
              </apex:pageBlockTable>
          </apex:pageblockSection>
      </apex:pageBlock>
    </apex:form>
</apex:page>
apex :
public with sharing class OppExtension {
    
    public List<opportunity> opportunityLst{get;set;}
    public List<OpportunityLineItem> oproducts{get;set;}
    public Integer productSize{get; set;}
    
    public OppExtension() {
       opportunityLst = [select id,Name,Account.name,(select id,Product2.Name,ListPrice,Quantity,TotalPrice from opportunitylineitems) from opportunity];
            
    }   
    
}
output:
User-added image

i hope it helps you.
      Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others forums best practice (https://developer.salesforce.com/forums/ForumsMain?id=9060G000000MVrtQAG)
 thanks
sfdcmonkey.com

All Answers

Rishiraj Singh 17Rishiraj Singh 17
My Code is:

<apex:page standardController="Opportunity" recordSetVar="opportunity" extensions="OppExtension">
    <apex:form >
      <apex:pageBlock >
          <apex:pageblockSection title="Opportunities Summary Details" collapsible="true" Columns="10">
              <apex:pageBlockTable value="{!opportunity}" var="opp">
                  <apex:column value="{!opp.Name}"/>
                  <apex:column value="{!opp.Account.name}"/>
                  <apex:column headerValue="Product Details">
                  <apex:pageBlockTable value="{!opp.opportunityLineItems}" var="opplt">
                      <apex:column value="{!opplt.Product2.Name}"/>
                      <apex:column value="{!opplt.ListPrice}"/>
                      <apex:column value="{!opplt.Quantity}"/>
                      <apex:column value="{!opplt.TotalPrice}"/>
                  </apex:pageBlockTable>
                  </apex:column>
                  <apex:column headerValue="Number Of Products">
                      <apex:outputText value="{!productSize}"></apex:outputText>
                  </apex:column>
              </apex:pageBlockTable>
          </apex:pageblockSection>
      </apex:pageBlock>
    </apex:form>
</apex:page>



public with sharing class OppExtension {
    private Opportunity oppt;
    public Integer productSize{get; set;}
    public OppExtension(ApexPages.StandardSetController controller) {
        this.oppt = (Opportunity)Controller.getRecord();
            productSize =[select count() from PricebookEntry where Id IN (Select PricebookentryId from opportunityLineItem where opportunityId=:oppt.id)];
            
    }   
    
}



But everytime Count() appears as 0... Please help!
sfdcMonkey.comsfdcMonkey.com
hi rishairaj ,
use below code :
 
<apex:page controller="OppExtension">
    <apex:form >
      <apex:pageBlock >
          <apex:pageblockSection title="Opportunities Summary Details" collapsible="true" Columns="10">
              <apex:pageBlockTable value="{!opportunityLst}" var="opp">
                  <apex:column value="{!opp.Name}"/>
                  <apex:column value="{!opp.Account.name}"/>
                  <apex:column headerValue="Product Details">
                  <apex:pageBlockTable value="{!opp.opportunitylineitems}" var="opplt">
                      <apex:column value="{!opplt.Product2.Name}"/>
                      <apex:column value="{!opplt.ListPrice}"/>
                      <apex:column value="{!opplt.Quantity}"/>
                      <apex:column value="{!opplt.TotalPrice}"/>
                  </apex:pageBlockTable>
                  </apex:column>
                  <apex:column headerValue="Number Of Products">
                      <apex:outputText value="{!opp.opportunitylineitems.size}"></apex:outputText>
                  </apex:column>
              </apex:pageBlockTable>
          </apex:pageblockSection>
      </apex:pageBlock>
    </apex:form>
</apex:page>
apex :
public with sharing class OppExtension {
    
    public List<opportunity> opportunityLst{get;set;}
    public List<OpportunityLineItem> oproducts{get;set;}
    public Integer productSize{get; set;}
    
    public OppExtension() {
       opportunityLst = [select id,Name,Account.name,(select id,Product2.Name,ListPrice,Quantity,TotalPrice from opportunitylineitems) from opportunity];
            
    }   
    
}
output:
User-added image

i hope it helps you.
      Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others forums best practice (https://developer.salesforce.com/forums/ForumsMain?id=9060G000000MVrtQAG)
 thanks
sfdcmonkey.com
This was selected as the best answer