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
Chris Young 2Chris Young 2 

Save a temporary list from a wrapper class table


Hi everyone,

I have a Visualforce page that uses a wrapper class to allow a user to see a list of equipment items available for purchase, check items from that list, press an 'add to order' button at the bottom of the list, then the checked items in the list are moved down to a table. I want to be able to save this list of checked items as a new purchase order consisting of these items.

Here is what I have so far:

Visualforce Page:
 
<apex:page controller="AccountSelectClassController">
<script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
<apex:form >
<apex:pageBlock >
    <apex:pageBlockSection showHeader="True" title="Purchase Items" columns="1">
                           
            <apex:pageBlockTable value="{!wrapPurchaseItemList}" var="accWrap" id="table">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column value="{!accWrap.acc.Description__c}" />
                    <apex:column value="{!accWrap.acc.Price__c}" />
                    <apex:column value="{!accWrap.acc.name}" />
                </apex:pageBlockTable>
            <apex:CommandButton value="Add to Order" action="{!processSelected}" rerender="requisitionitems" />
            
        
     </apex:pageBlockSection>
     <apex:pageBlockSection showHeader="True" title="Requisition Items" columns="1">
        
            
             <apex:pageBlockTable value="{!selectedPurchaseItems}" var="c" id="requisitionitems" title="Selected Accounts">
                    <apex:column id="Description" value="{!c.Description__c}" headerValue="Description"/>
                    <apex:column id="Price" value="{!c.Price__c}" headerValue="Price"/>
                    <apex:column id="Item_Code" value="{!c.name}" headerValue="Item Code"/>
                </apex:pageBlockTable>
            <apex:CommandButton value="Save" action="{!save}" />
           
               
     </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Apex Class:
 
public class AccountSelectClassController{
 
    //Our collection of the class/wrapper objects wrapAccount
    public List <Purchase_Item__c> poitemList {get;set;}
    public List <Purchase_Item__c> addPOItemList {get;set;}
    public List <Purchase_Item__c> delPOItemList {get;set;}
    public List <Purchase_Item__c> delPOItems {get;set;}
    public Integer totalCount {get;set;}
    public List<wrapPurchaseItem> wrapPurchaseItemList {get; set;}
    public List<Purchase_Item__c> selectedPurchaseItems{get;set;}
    public void AddPOItems(ApexPages.StandardController controller) {
                purchords = (Purchase_Order__c) controller.getRecord();
                poitemList = [Select id, Description__c, Item_Code__c, Price__c, Purchase_Order__c, Vendor__c from Purchase_Item__c where Purchase_Order__c = : purchords.Id];
                totalCount = poitemList.size();
                delPOItemList = new List < Purchase_Item__c > ();
                delPOItems = new List < Purchase_Item__c > ();
                
        }
    public AccountSelectClassController(){
        if(wrapPurchaseItemList == null) {
            wrapPurchaseItemList = new List<wrapPurchaseItem>();
            for(Purchase_Item__c a: [select Id, Description__c,Price__c, name from Purchase_Item__c limit 10]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapPurchaseItemList.add(new wrapPurchaseItem(a));
            }
        }
    }
 
    public void processSelected() {
    selectedPurchaseItems = new List<Purchase_Item__c>();
 
        for(wrapPurchaseItem wrapPurchaseItemObj : wrapPurchaseItemList) {
            if(wrapPurchaseItemObj.selected == true) {
                selectedPurchaseItems.add(wrapPurchaseItemObj.acc);
            }
        }
    }
   
    public Purchase_Order__c purchords;
    
    public PageReference save() {
                upsert selectedPurchaseItems;
                
                //return (new ApexPages.StandardController(purchreqs)).view();
                PageReference pageRef = new PageReference('/apex/PurchaseItemList');
                pageRef.getParameters().put('id',purchords.Id);
                return pageRef;
        }

    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapPurchaseItem {
        public Purchase_Item__c acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapPurchaseItem(Purchase_Item__c a) {
            acc = a;
            selected = false;
        }
    }
}
Thanks in advance!