-4

i created custom button on opportunity object which will redirect user to a VF page which will populate some opportunity fields and a table showing related opportunity product details

<apex:page standardController="Opportunity"   extensions="PDFcontroller"  >
    <apex:form >
        <apex:pageBlock >
            <apex:pageblockSection title="Opportunity">
                <apex:outputText value="Select opportunity"></apex:outputText>
                <apex:selectList value="{!selval}" size="1">
                    <apex:selectOptions value="{!AllOpportunity}">
                    </apex:selectOptions> 
                </apex:selectList>
                <apex:commandButton value="ViewProducts" action=" {ShowRelatedProducts}"/>
            </apex:pageblockSection>
        </apex:pageBlock>
        <apex:pageBlock >
            <apex:pageblockSection title="Products">
                <apex:pageBlockTable value="{!opportunity.opportunityLineItems}" var="prd">
                    <apex:column value="prd.ServiceDate"/>
                    <apex:column value="prd.Discount"/>
                    <apex:column value="prd.ListPrice"/>
                    <apex:column value="prd.ProductCode"/>
                    <apex:column value="prd.Quantity"/>
                    <apex:column value="prd.UnitPrice"/>
                    <apex:column value="prd.Subtotal/>
                    <apex:column value="prd.TotalPrice"/>
                </apex:pageBlockTable>
            </apex:pageblockSection>
    </apex:pageBlock>  
    </apex:form>
</apex:page>

please suggest me the controller code to display products details

CONTROLLER:

public class PDFcontroller {
    public String selval{get;set;}
    public list<selectOption> AllOpportunity  {get; set;}
    public List<OpportunityLineItem> products {get; set;} 

    public PDFcontroller(ApexPages.StandardController controller) {
        AllOpportunity  =new list<selectOption>();
        list<opportunity> optns=[select id,name  from opportunity];
        for(opportunity op:optns){
            AllOpportunity.add(new selectOption(op.id,op.name));
        }
    }

    public void ShowRelatedProducts()
    {
        List <OpportunityLineItem> prod_InfoList = [SELECT ServiceDate, 
                                                           Discount, ListPrice, 
                                                           ProductCode,Quantity,
                                                           UnitPrice,Subtotal,TotalPrice 
                                                    FROM OpportunityLineItem
                                                    WHERE op.id=:selval];
    }
}

am new to coding but trying to do coding like this..please suggest me to resolve

3
  • Perhaps you could post some controller code that you have already written and let us know where you are having trouble? You'll get a better response with a more focused question.
    – Mark Keats
    Apr 8, 2016 at 7:54
  • and your question is?
    – Novarg
    Apr 8, 2016 at 8:45
  • product details r not populating while clicking that viewconatact button.
    – lathish
    Apr 8, 2016 at 9:44

2 Answers 2

1

The problem is you are assigning the query to a local variable called prod_InfoList but using opportunity.opportunityLineItems in <apex:pageBlockTable>.

So i suggest you to change the showRelatedProducts method to

public void ShowRelatedProducts()
{
    products = [SELECT ServiceDate,   Discount, ListPrice, ProductCode,Quantity,UnitPrice,Subtotal,TotalPrice 
                                                FROM OpportunityLineItem
                                                WHERE op.id=:selval];
}

and the <apex:pageBlockTable> to

<apex:pageBlockTable value="{!products}" var="prd">
    <apex:column value="prd.ServiceDate"/>
    <apex:column value="prd.Discount"/>
    <apex:column value="prd.ListPrice"/>
    <apex:column value="prd.ProductCode"/>
    <apex:column value="prd.Quantity"/>
    <apex:column value="prd.UnitPrice"/>
    <apex:column value="prd.Subtotal/>
    <apex:column value="prd.TotalPrice"/>
</apex:pageBlockTable>
2
  • yeahh..now errors gone but still products details r not dispalyingg,even opportunitys having multiple products records..
    – lathish
    Apr 8, 2016 at 10:59
  • put a debug under products query to check whether it queries any records or not. Apr 8, 2016 at 11:01
0

This is my controller and vfpage,its compiled successfully but not displayng any product values after clicking custom button

VF page:

       <apex:page standardController="Opportunity"   extensions="PDFcontroller" >
     <apex:form >
      <apex:pageBlock >
     <apex:pageblockSection title="Opportunity">
       <apex:outputText value="Select opportunity"></apex:outputText>
       <apex:selectList value="{!selval}" size="1">
           <apex:selectOptions value="{!AllOpportunity}">
           </apex:selectOptions> 
           </apex:selectList>

         <apex:commandButton value="ViewProducts" action="{!ShowRelatedProducts}"/>
    </apex:pageblockSection>
</apex:pageBlock>
        <apex:pageBlock >
      <apex:pageblockSection title="Products">

           <apex:pageBlockTable value="{!opportunity.opportunityLineItems}" var="prd">

          <apex:column value="{!prd.ServiceDate}"/>
          <apex:column value="{!prd.Discount}"/>
          <apex:column value="{!prd.ListPrice}"/>
          <apex:column value="{!prd.ProductCode}"/>
          <apex:column value="{!prd.Quantity}"/>
          <apex:column value="{!prd.UnitPrice}"/>
          <apex:column value="{!prd.Subtotal}"/>
          <apex:column value="{!prd.TotalPrice}"/>

        </apex:pageBlockTable>
    </apex:pageblockSection>
    </apex:pageBlock> 
    </apex:form>
   </apex:page>

CONTROLLER:

 public class PDFcontroller {
  public String selval{get;set;}
  public list<selectOption> AllOpportunity  {get; set;}
  public List<OpportunityLineItem> products {get; set;} 

  public PDFcontroller(ApexPages.StandardController controller) {


// getting opportunity information
  AllOpportunity  =new list<selectOption>();
  list<opportunity> optns=[select id,name  from opportunity];
 for(opportunity op:optns){
 AllOpportunity.add(new selectOption(op.id,op.name));

}
}

//getting product details
  public void ShowRelatedProducts()
 { 
  products=new List<OpportunityLineItem>();
 for(opportunityLineItem product: [SELECT ServiceDate, Discount, ListPrice,  ProductCode,
                                   Quantity,UnitPrice,Subtotal,TotalPrice 
                                   FROM opportunityLineItem WHERE     opportunityid=:selval]){

         products.add(product);         
         System.debug('product details');                 
                                            }
    }
  }
2
  • You are still using opportunity.opportunityLineItems but it should be products. Apr 8, 2016 at 12:56
  • k..i ll replace it with products..
    – lathish
    Apr 8, 2016 at 13:08

You must log in to answer this question.

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