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
wadams2010wadams2010 

Insert Selected Records From Wrapper Class

Hey everybody!

I am trying to create records (Associated Locations) based off of a user selected list of a custom object (Site). I have tried following a number of guides on how to do this but can't really follow. Right now when I try and save values with a debug catch it shows the number of records I have selected but the values are null. How can I add values and insert the records? Thansk in advance!

Apex: 
public with sharing class quoteAssociatedSiteExtension {

	//Our collection of the class/wrapper objects cContact 
	public Quote theQuote {get; set;}
    public List<assLoc> assLocList {get; set;}
    
    
    public quoteAssociatedSiteExtension(ApexPages.StandardController controller) { 

        String quoteId = ApexPages.currentPage().getParameters().get('quoteId');
		theQuote = [select Id, AccountId from Quote where Id =:quoteId limit 1];
    }

	//This method uses a simple SOQL query to return a List of Contacts
	public List<assLoc> getAssLoc() {
		if(assLocList == null) {
			assLocList = new List<assLoc>();
			for(Site__c s: [select Id, Name, Account__r.Id from Site__c where Account__r.Id=:theQuote.AccountId limit 10]) {
				// As each contact is processed we create a new cContact object and add it to the contactList
				assLocList.add(new assLoc(s));
			}
		}
		return assLocList;
	}


	public PageReference processSelected() {

                //We create a new list of Contacts that we be populated only with Contacts if they are selected
		List<Associated_Location__c> selectedAssociatedLocations = new List<Associated_Location__c>();

		//We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
		for(assLoc aLoc: getAssLoc()) {
			if(aLoc.selected == true) {
				selectedAssociatedLocations.add(aLoc.ac);
			}
		}

		// Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
		System.debug('These are the selected Contacts...');
		for(Associated_Location__c ac: selectedAssociatedLocations) {
			system.debug(ac);
		}
		assLocList=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
		return null;
	}


	// This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
	public class assLoc {
		public Associated_Location__c ac {get; set;}
        public Site__c st {get; set;}
        public Quote qt {get; set;}
		public Boolean selected {get; set;}

		//This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
		public assLoc(Associated_Location__c a) {
			ac = a;
			selected = false;
		}
        
        public assLoc(Site__c s) {
			st = s;
			selected = false;
		}
        
		public assLoc(Quote q) {
			qt = q;
			selected = false;
		}        
	}
}

VF Page: 
 
<apex:page standardController="Associated_Location__c" extensions="quoteAssociatedSiteExtension">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
            </apex:pageBlockButtons>
            <!-- In our table we are displaying the cContact records -->
            <apex:pageBlockTable value="{!AssLoc}" var="a" id="table">
                <apex:column >
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!a.selected}"/>
                </apex:column>
                <!-- This is how we access the contact values within our cContact container/wrapper -->
                <apex:column value="{!a.st.Name}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>