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
NehaKSNehaKS 

Adding more line items to existing opportunity using a vf page

 

 

 

Hi all,

 

I want to show the list of the existing line items for the opportunity i a page block table and have an add button to add more line items.

 

I am getting error when i run this code. Also i am not able to pull the existing line items.

 

public class AddRowsOpli     
{  
    public List<OpportunitylineitemWrapper> wrappers {get; set;}  
    public static Integer toDelIdent {get; set;}  
    public static Integer addCount {get; set;}  
    private Integer nextIdent=0;     
    public AddrowsOpli() 
     {   wrappers=new List<OpportunitylineitemWrapper>();   
         for (Integer idx=0; idx<5; idx++)   
         {    
            wrappers.add(new OpportunitylineitemWrapper(nextIdent++));   
         }  
      }     
      public void delWrapper()  
      {   
           Integer toDelPos=-1;   
           for (Integer idx=0; idx<wrappers.size(); idx++)   
           {    
                if (wrappers[idx].ident==toDelIdent)    
                {    
                     toDelPos=idx;    
                }   
           }       
            if (-1!=toDelPos)   
            {    
              wrappers.remove(toDelPos);   
            }  
       }     
       public void addRows() 
        {   
          for (Integer idx=0; idx<addCount; idx++)   
          {    
               wrappers.add(new OpportunitylineitemWrapper(nextIdent++));   
           }  
        }     
        public PageReference save()  
        {   
               List<Opportunitylineitem> opps=new List<Opportunitylineitem>();   
               for (OpportunitylineitemWrapper wrap : wrappers)   
               {    
                     opps.add(wrap.opp);   
               }       
               insert opps;       
               return new PageReference('/' + Schema.getGlobalDescribe().get('Opportunity').getDescribe().getKeyPrefix() + '/o');  
         }    
         public class OpportunitylineitemWrapper 
         {  
           
          public Opportunitylineitem opp {get; private set;}   
          public Integer ident {get; private set;}       
          public OpportunitylineitemWrapper(Integer inIdent)   
               {    
             ident=inIdent;    
             opp=new Opportunitylineitem(opportunityId=ApexPages.currentPage().getParameters().get('id'));  
/*             OpportunityLineItem i = new OpportunityLineItem();
        i.opportunityId       = o.id;
        i.pricebookentryid    = pbe.id;
        i.quantity            = 1;
        i.unitprice           = 1;
 opp = [SELECT Amount, Id, Name,CloseDate,StageName,Account.Name,Probability, (SELECT Quantity,  PricebookEntry.Name, Description, UnitPrice,ServiceDate FROM OpportunityLineItems) FROM Opportunity where id =:ApexPages.currentPage().getParameters().get('id')];
 o=[SELECT opportunityid,Quantity,  PricebookEntry.Name, Description, UnitPrice,ServiceDate, (Select OpportunityLineItem.PricebookEntry.Product2.Name, Id, OpportunityLineItemId,ScheduleDate,Revenue, Quantity, Description From OpportunityLineItemSchedules) FROM OpportunityLineItem where opportunityid=:ApexPages.currentPage().getParameters().get('id')];      
       
       
        */
 
               } 
           } 
}

 Page code

<apex:page controller="AddRowsOpli" tabstyle="Opportunity">  
<apex:form >    
<apex:pageBlock title="Bulk Opportunitylineitem Create">       
<apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">          
<apex:column headerValue="Ident">             
<apex:outputText value="{!wrapper.ident}"/>          
</apex:column>          
<apex:column headerValue="Name">             
<apex:inputField value="{!wrapper.opp.opportunity.name}"/>          
</apex:column>          
<apex:column headerValue="Quantity">             
<apex:inputField value="{!wrapper.opp.Quantity}"/>          
</apex:column>          
<apex:column headerValue="PricebookEntry">             
<apex:inputField value="{!wrapper.opp.PricebookEntryId}"/>         
</apex:column>         
<apex:column headerValue="UnitPrice">             
<apex:inputField value="{!wrapper.opp.UnitPrice}"/>       
</apex:column>
<apex:column headerValue="ServiceDate">             
<apex:inputField value="{!wrapper.opp.ServiceDate}"/>       
</apex:column>   

<apex:column headerValue="Action">             
<apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">                
<apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/>              
</apex:commandButton>          
</apex:column>       
</apex:pageBlockTable>       
<apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">         
<apex:param name="addCount" value="1" assignTo="{!addCount}"/>        
</apex:commandButton>       
<apex:commandButton value="Add 5 Rows" action="{!addRows}" rerender="wtable">          
<apex:param name="addCount" value="5" assignTo="{!addCount}"/>        
</apex:commandButton>       
<apex:commandButton value="Save" action="{!save}"/>   
</apex:pageBlock> 
</apex:form> 
</apex:page>