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
vdanvoye6596vdanvoye6596 

List of opportunity products - VF

Is there  away to overwrite the "standard" list of Opportunity Products (line items) with a Visual Force page.  I know the opportunity objects are somewhat special - could not find a way to achieve that.  Thanks.
Ajay K DubediAjay K Dubedi
Hi Vdanvoye, 

Below Sample code can fulfill your requirements. Hope this will work for you.
<apex:page standardController="Opportunity" extensions="CreateQuoteClass">
    <apex:sectionHeader title="Opportunity Items"/>
    <apex:form>
        <apex:pageBlock title="Products Items">
            <apex:pageBlockTable var="qi" value="{!quotewrapperlist}" id="quoteitem">
                <apex:column headerValue="Action">
                    <apex:inputCheckbox value="{!qi.isChecked}"/>
                </apex:column>
                <apex:column headerValue="Product" value="{!qi.oliresult.name}"/>
                <apex:column headerValue="Quantity" value="{!qi.oliresult.Quantity}"/>
                <apex:column headerValue="Sales Price" value="{!qi.oliresult.Unitprice}"/>
                <apex:column headerValue="List Price" value="{!qi.oliresult.TotalPrice}"/>
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Create Quote" action="{!saveQuote}" immediate="false"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>


public class CreateQuoteClass {
    public String opportunitystringId {get;set;}
    public List<Opportunity> opportunityList {get;set;}
    public List<quotewrapper> quotewrapperlist {get;set;}
    public List<OpportunityLineItem> oliList{get;set;}
    
    public class quotewrapper
    {
        public Boolean isChecked {get;set;}
        public OpportunityLineItem oliresult {get;set;}
        
        public quotewrapper(Boolean isChecked, OpportunityLineItem oliresult)
        {
            This.isChecked = isChecked;
            This.oliresult = oliresult;
        } 
    }   
    
    public CreateQuoteClass(ApexPages.StandardController controller)
    {
        try{
            quotewrapperlist = new List<quotewrapper>();
            opportunitystringId  = ApexPages.CurrentPage().getparameters().get('Id');
            
            if(opportunitystringId!=null)
            {
                opportunityList = [SELECT Id,Name,CloseDate,AccountId,Pricebook2Id from Opportunity WHERE Id =:opportunitystringId];
                oliList = [Select Id,Name,Quantity,OpportunityId,UnitPrice,Product2Id,PricebookentryId,TotalPrice from OpportunityLineItem WHERE OpportunityId =:opportunitystringId];
            }
            
            if(oliList.size()>0)
            {
                for(OpportunityLineItem olObj:oliList)
                {
                    quotewrapper qobj =  new quotewrapper(false, olObj);
                    quotewrapperlist.add(qobj);
                }
            }
        }
        catch(Exception e)
        {
            System.debug('The following exception has occurred: ' + e.getMessage());
        }
        
    }
    
    public void saveQuote()
    {
        try{
            List<OpportunityLineItem> olilistNew = new List<OpportunityLineItem>();  
            List<QuoteLineItem>  quoteItemList = new List<QuoteLineItem>();
            
            for(quotewrapper qwr : quotewrapperlist)
            {
                if(qwr.isChecked==true)
                {
                    olilistNew.add(qwr.oliresult);
                }
            }
            
            List<Quote> quoteListNew = new List<Quote>();
            if(opportunityList.size()>0)
            {
                for(Opportunity opportunityObj : opportunityList)
                {
                    Quote quoteObj = new Quote();
                    quoteObj.Name=opportunityObj.Name;
                    quoteObj.OpportunityId = opportunityObj.Id;
                    quoteObj.Pricebook2Id =opportunityObj.Pricebook2Id;
                    quoteListNew.add(quoteObj); 
                }
            }
            if(quoteListNew.size()>0)
            {
                Database.SaveResult[] quoteItemSaveList = Database.insert(quoteListNew);
            }
            if(olilistNew.size()>0)
            {
                for(Quote qutObj: quoteListNew)
                { 
                    for(OpportunityLineItem oliObj : olilistNew)
                    {  
                        QuoteLineItem qlobj = new QuoteLineItem();
                        qlobj.Quantity=oliObj.Quantity;
                        qlobj.PricebookEntryId=oliObj.PricebookEntryId;
                        qlobj.QuoteId=qutObj.Id;
                        qlobj.Product2Id=oliObj.Product2Id;
                        qlobj.UnitPrice=oliObj.UnitPrice;
                        quoteItemList.add(qlobj);   
                    }
                }
                
                if(quoteItemList.size()>0)
                {
                    Database.SaveResult[] quoteItemSaveList = Database.insert(quoteItemList);
                }
            }
        } 
        
        catch(Exception e)
        {
            System.debug('The following exception has occurred: ' + e.getMessage());
        }  
    }
}

Please mark this as best answer if this solves your problem.

Thank you,
Ajay Dubedi
vdanvoye6596vdanvoye6596
Thanks for your reply, Ajay.  In fact, I already have the Apex Class & the VF page.  They do work.  I just can't seem to be able to "overwrite" the standard list of Opportunity products.
 
public class myOpptyController {

    public List<Opportunity> opptyList;
    
    public myOpptyController() {

    opptyList = [SELECT Id,Name,Account.Name, 
                                  (SELECT Quantity, UnitPrice, TotalPrice,PricebookEntry.Name FROM OpportunityLineItems) 
                        FROM Opportunity WHERE Id =: ApexPages.currentPage().getParameters().get('opptyID')];
    
        System.debug('opptyList ='+opptyList);

    }
    
    public List<Opportunity> getopptyList() {
        return opptyList;
    }
    
}


<apex:page standardController="Opportunity" extensions="OppLineListCon" readOnly="true" tabStyle="Opportunity">
    <head>
        <apex:includescript value="//code.jquery.com/jquery-1.11.1.min.js" / >
        <apex:includescript value="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js" />
        <apex:stylesheet value="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css" />
        <script>
            j$ = jQuery.noConflict();
            j$(document).ready( function () {
                var productsTable = j$('[id$="productsTable"]').DataTable({
                    
                });
            });
        </script>
    </head>
    <body>
    <apex:pageBlock title="Related Products">

        <table id="productsTable" class="display">
            <thead>
                <tr>
                    <th>Action</th>
                    <th>Outing</th>
                    <th>Start Date</th>
                    <th>Stage</th>
                    <th>Product Name</th>
                    <th>Product Code</th>
                    <th>Location</th>
                    <th>Price</th>
                    <th>Description</th>
                </tr>
            </thead>
            <tbody>
                <apex:repeat value="{!LineItems}" var="oppLineItem">
                    <tr>
                        <td>
                        <apex:outputLink value="/{!oppLineItem.id}/e?retURL=/apex/{!$CurrentPage.Name}">Edit</apex:outputLink>&nbsp;|&nbsp;
                        <apex:outputLink value="/{!oppLineItem.id}">Read</apex:outputLink>
                        </td>
                        <td>{!oppLineItem.Outing_Seq__c}</td>
                        <td>
                            <apex:outputText value="{0, date, MMMM d','  yyyy}">
                                <apex:param value="{!oppLineItem.Start_Date__c}" /> 
                            </apex:outputText>                        
                        </td>
                        <td>{!oppLineItem.Opportunity_Product_Stage__c}</td>
                        <td>{!oppLineItem.PricebookEntry.Product2.Name}</td>
                        <td>{!oppLineItem.ProductCode}</td>
                        <td>{!oppLineItem.Location__c}</td>
                        <td>{!oppLineItem.UnitPrice}</td>
                        <td>{!oppLineItem.Description}</td>
                   
                    </tr>
                </apex:repeat>
            </tbody>
        </table>
        </apex:pageblock>
    </body>

</apex:page>