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 

Input Field on Wrapper Class Not Passing Inputs

I am having an issue passing an input field from a Visualforce page to new inserted records. The field I am trying to pass is 'Remarks_Special_Instructions__c' but when I put info in there it is not saving into the newly created records. I have tried a few different ways but no luck on any front. What are my issues? Thanks 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, City__c, Country__c,Order_Master_ID__c, Order_Master_Link__c, Site_ID__c, Site_Main_Phone__c, State__c, Street__c, Zip__c
                            from Site__c 
                            where Account__r.Id=:theQuote.AccountId
                           ]) {
				// 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<Site__c> selectedAssociatedLocations = new List<Site__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.st);
			}
		}

		// 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
		List<Associated_Location__c> nmal = new List<Associated_Location__c>();
        for(Site__c al: selectedAssociatedLocations) {          
            Associated_Location__c nal = new Associated_Location__c();
            nal.Site__c = al.Id;
            nal.Quote__c = theQuote.Id;
            nmal.add(nal);
		}
		insert nmal; 
        return new PageReference('/'+ApexPages.currentPage().getParameters().get('quoteId'));
	}


	// 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;
		}        
	}
}

Visualforce:
<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.Street__c}" />
                <apex:column value="{!a.st.City__c}" />
                <apex:column value="{!a.st.State__c}" />
                <apex:column value="{!a.st.Zip__c}" />
                <apex:column headerValue="{!$ObjectType.Associated_Location__c.Fields.Remarks_Special_Instructions__c.Label}">
                    <apex:inputField value="{!a.ac.Remarks_Special_Instructions__c}" required="false"/>
                </apex:column>
               	<apex:column value="{!a.st.Site_Main_Phone__c}" />
                
                <apex:column value="{!a.st.Order_Master_Link__c}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
Naresh YadavNaresh Yadav
Hi Will Adams

Please check Field level security and object level security(Create,Edit..) for current user.
wadams2010wadams2010
That shouldn't be an issue, all the fields have the correct security levels and I am set-up as the admin. I think it has to do with fact that I am not setting the values in my apex code correctly and therefore they are not being passed over to the new records. Any other ideas?
John BrodrickJohn Brodrick
So in the method processSelected() in the loop starting at line 34 you loop through all the assLoc wrappers and pull out the site__c info but don't do anything with the associated_location__c.

Try something like this:
 
public PageReference processSelected() {
		List<Associated_Location__c> nmal = new List<Associated_Location__c>();
        
		for(assLoc aLoc: getAssLoc) {
			if(aLoc.selected == true) {   
                Associated_Location__c nal = new Associated_Location__c();
                
                nal.Remarks_Special_Instructions__c = aLoc.ac.Remarks_Special_Instructions__c;
                nal.Site__c = aLoc.st.Id;
                nal.Quote__c = theQuote.Id;
                
                nmal.add(nal);
			}
		}
        
        if(nmal.size()>0)
            insert nmal; 
        
        return new PageReference('/'+ApexPages.currentPage().getParameters().get('quoteId'));
	}

 
wadams2010wadams2010
@John Brodrick,

That feels pretty close but it is still not taking the inputfield values. Do I need to set those somewhere? This is the first slightly large apex/visualforce code I have written and have no real developer experience so I honestly have no clue what I am doing. Thanks in advance!
John BrodrickJohn Brodrick
I am only seeing one inputfield on there - the one for Remarks_Special_Instructions__c - and that should be getting set unless my code isn't working.  Am I missing some other inputfields?
wadams2010wadams2010
No that is the only input field. I was going to add another one after I got this one to work. 
John BrodrickJohn Brodrick

So you're saying the Remarks_Special_Instructions__c data still ins't getting set?

Try doing some print statements to debug it.

Put System.debug statements into the processSelected method and print out a bunch of things to see what the values are.  Let me know if you need help on how to do this.

wadams2010wadams2010
The values are coming back as null. I did both a debug and looked at the logs. By the way, below is what I chagned the VF to:
<apex:column headerValue="{!$ObjectType.Associated_Location__c.Fields.Remarks_Special_Instructions__c.Label}">
                    <apex:inputField value="{!a.ac.Remarks_Special_Instructions__c}" required="false"/>
                </apex:column>

 
John BrodrickJohn Brodrick

In the wrapper class, the constructors don't initialize all of the variables.  Specifically in the one that takes a site (the one you are using in this code) the associated_location__c variable "ac" would be left null since you didnt instantiate it.  Try instantiating all of the variables in your constructors:

...

ac = new Associated_Location__c();
..

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_variables.htm

See the section on null variables and initial values.

John BrodrickJohn Brodrick
Hey WillAdams7 - are you still having issues with this?  Can you mark it as solved if not?