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
Glen.ax1034Glen.ax1034 

Best Practice: Checkbox list and passing into apex.

I have a apex visualforce page / apex that runs code to lookup relevant products (Product2) to potentially tie back to a specific Package (Packages__c) using a many:many relationship called ProductPackage.

 

So far, I've been successfully able to create the code/logic to pull everything necessary to the visual force page. Now, I am trying to figure out the best way to pass information from a form with a custom button back into apex and get a list of the selected Product2's.

 

What is the best strategy to use checkboxes to pull a list of these product2.ID's as well as potentially some sort of specific percent discount by product back into the apex environment so I can run them through more, yet to be developed algorithms/methods?

 

 

 

 

<apex:page standardController="Packages__c" extensions="packageextensions">

Package Name: <apex:OutputText value="{!Packages__c.Name}" />
<apex:form >
    <table border="1" >

        <tr>

            <th></th>
            <apex:repeat value="{!gProducts}" var="Nam">
            <th>{!Nam.Name}</th>
            </apex:repeat> 
        </tr>
        
        <tr>
            <td>Product Id</td>
        <apex:repeat value="{!gProducts}" var="Nam">
            <td>{!Nam.Id}<apex:inputCheckbox value="{!Nam.Id}"/></td>
        </apex:repeat> 
        </tr>
        
        
        <tr>
            <td>Price Book Id</td>
                <apex:repeat value="{!gPricebookEntry}" var="Nam">
                    <apex:repeat value="{!Nam.OpportunityLineItems}" var="OLI">
                        <td>{!OLI.Id}</td>
                    </apex:repeat> 
                </apex:repeat> 
        </tr>
        
        
        <tr>
            <td>Quantity</td>
                <apex:repeat value="{!gPricebookEntry}" var="Nam">
                    <apex:repeat value="{!Nam.OpportunityLineItems}" var="OLI">
                        <td>{!OLI.Quantity}</td>
                    </apex:repeat> 
                </apex:repeat> 
        </tr>
        
        
        <tr>
            <td>Default Price</td>
                <apex:repeat value="{!gPricebookEntry}" var="Nam">
                    <apex:repeat value="{!Nam.OpportunityLineItems}" var="OLI">
                        <td>{!OLI.ListPrice}</td>
                    </apex:repeat> 
                </apex:repeat> 
        </tr>
        
        
        <tr>
            <td>Unit Price</td>
                <apex:repeat value="{!gPricebookEntry}" var="Nam">
                    <apex:repeat value="{!Nam.OpportunityLineItems}" var="OLI">
                        <td>{!OLI.UnitPrice}</td>
                    </apex:repeat> 
                </apex:repeat> 
        </tr>       

        <tr>
            <td>Total Price</td>
                <apex:repeat value="{!gPricebookEntry}" var="Nam">
                    <apex:repeat value="{!Nam.OpportunityLineItems}" var="OLI">
                        <td>{!OLI.TotalPrice}</td>
                    </apex:repeat> 
                </apex:repeat> 
        </tr>
        
        <tr>
            <td>Package</td>
                <apex:repeat value="{!gPricebookEntry}" var="Nam">
                    <apex:repeat value="{!Nam.OpportunityLineItems}" var="OLI">
                        <td>{!OLI.Package_Name__c}</td>
                    </apex:repeat> 
                </apex:repeat> 
        </tr>
        
        <tr>
            <td>Description</td>
                <apex:repeat value="{!gPricebookEntry}" var="Nam">
                    <apex:repeat value="{!Nam.OpportunityLineItems}" var="OLI">
                        <td>{!OLI.Item_Description__c}</td>
                    </apex:repeat> 
                </apex:repeat> 
        </tr>
        
        <tr>
            <td>Include</td>
            
                <apex:repeat value="{!gPricebookEntry}" var="Nam">
                    <apex:repeat value="{!Nam.OpportunityLineItems}" var="OLI">
                        <td>{!OLI.Item_Description__c}</td>
                    </apex:repeat> 
                </apex:repeat> 
               
        </tr>

            
    </table>
    
     <apex:commandButton action="{!savepackage}" value="Save Package"/>
</apex:form>
</apex:page>

 

public class packageextensions {
    private final Packages__c PackageObj;
   // public pagereference savepackage(){}
    public packageextensions (ApexPages.StandardController controller) {
        this.PackageObj= (Packages__c)controller.getSubject();
        
    }
    
    
  //     public List<Product2> setgtest() {
  //    if(account == null) ppackage = new Packages__c();
  //    return ppackage;
  // }
    public List<Product2> includeinpackage{get;set;}
   public pagereference savepackage()
{
    
    includeinpackage=includeinpackage;
return null;
}


        
    public List<Product2> getgProducts() {
        //options.add(new Product2('None'));
    
        List<Product2> returnProducts = new List<Product2>();
        
        List<Packages__c> origPackage = new List<Packages__c>();
        origPackage = [select Opportunity__c from Packages__c where id=:PackageObj.Id Limit 1];
        
        List<OpportunityLineItem> origOpportunityLineItem = new List<OpportunityLineItem >();
        origOpportunityLineItem = [Select PricebookEntryId from OpportunityLineItem where OpportunityId =:origPackage[0].Opportunity__c];
        
        
        for(OpportunityLineItem presentopp : origOpportunityLineItem) {
            List<PricebookEntry> pbe = new List<PricebookEntry>();
            //pbe = [Select Product2Id from PricebookEntry where id=:presentopp.PricebookEntryId];
            
            //List<PricebookEntry> productpbe = new List<PricebookEntry>();
            pbe = [Select Product2Id, (Select Id, OpportunityId, PricebookEntryId, Quantity, TotalPrice, UnitPrice, ListPrice, Item_Description__c, Product_Line__c, Package_Name__c From OpportunityLineItems) From PricebookEntry p where id=:presentopp.PricebookEntryId];
            
            List<Product2> productitem = new List<Product2>();
            
            productitem = [Select Name, Id from Product2 where id=:pbe[0].Product2Id];
            returnProducts.add(productitem[0]);
            system.debug(productitem[0].Name);
            system.debug(productitem[0].Id);
            
            
            
            //I CAN'T SEEM TO GET TO THE SUBCOMPONENTS FROM THE QUERY 3 Lines Above!
        
        }
        
        
        
        //List<Product2> productitem2 = [Select Name, Id from Product2 where id=:pbe[0].Product2Id];
        
        return returnProducts;
        
        }

    public List<PricebookEntry> getgPricebookEntry() {
        //options.add(new Product2('None'));
    
        List<PricebookEntry> returnpbe = new List<PricebookEntry>();
        
        List<Packages__c> origPackage = new List<Packages__c>();
        origPackage = [select Opportunity__c from Packages__c where id=:PackageObj.Id Limit 1];
        
        List<OpportunityLineItem> origOpportunityLineItem = new List<OpportunityLineItem >();
        origOpportunityLineItem = [Select PricebookEntryId from OpportunityLineItem where OpportunityId =:origPackage[0].Opportunity__c];
     
        integer i, j;
        integer olicount=[Select COUNT() from OpportunityLineItem where OpportunityId =:origPackage[0].Opportunity__c];
        i=0; j=0;
        
        while(i<olicount) {
            List<PricebookEntry> pbe = new List<PricebookEntry>();
            //pbe = [Select Product2Id from PricebookEntry where id=:presentopp.PricebookEntryId];
            
            //List<PricebookEntry> productpbe = new List<PricebookEntry>();
            pbe = [Select Product2Id, (Select Id, OpportunityId, PricebookEntryId, Quantity, TotalPrice, UnitPrice, ListPrice, Item_Description__c, Product_Line__c, Package_Name__c From OpportunityLineItems) From PricebookEntry where id=:origOpportunityLineItem[i].PricebookEntryId];
 
            integer pbecount = [Select Count() From PricebookEntry where id=:origOpportunityLineItem[i].PricebookEntryId];
            j=0;
            while(j<pbecount) {
                returnpbe.add(pbe[j]);
                j++;
                i++;
            }           
            i++;
        }
        
        
     /*  
        for(OpportunityLineItem presentopp : origOpportunityLineItem) {
            List<PricebookEntry> pbe = new List<PricebookEntry>();
            //pbe = [Select Product2Id from PricebookEntry where id=:presentopp.PricebookEntryId];
            
            //List<PricebookEntry> productpbe = new List<PricebookEntry>();
            pbe = [Select Product2Id, (Select Id, OpportunityId, PricebookEntryId, Quantity, TotalPrice, UnitPrice, ListPrice, Item_Description__c, Product_Line__c, Package_Name__c From OpportunityLineItems) From PricebookEntry where id=:presentopp.PricebookEntryId];
            for(PricebookEntry presentpbe : pbe) {
                returnpbe.add(presentpbe);
                origOpportunityLineItem++;
            }           
        
        }
        */
        
        
        //List<Product2> productitem2 = [Select Name, Id from Product2 where id=:pbe[0].Product2Id];
        
        return returnpbe;
        
        }    
    
    
}