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

Use Input in Wrapper Class

I need to use the value from an inputField to populate a variable in a wrapper class. How do I that?





//Variable for input
public String duration { get; set; }

//Wrapper class needing variable

public class TotalTerr {
	public String TerritoryName { get; set; }
	//Class variable that needs input
	public String Duration { get; set; }



I could really use the help - thanks in advance!





    The inputField tag is very powerful to use with sObject fields rather that inner class members (prmitive data types). If the duration variable is a field in any sObject then you can use inputfield. Otherwise, if you are using String as datatype, then you need to use inputText, selectList tag to collect user input.

E.g.: If the variable Duration is a custom field in Account SObject then you need to code the inner class as below


//Wrapper class

public class TotalTerr{

         public String territoryName {get; set;}

         public Account acc {get; set;}



in VF page


<apex:inputField value = "{!ttInstance.acc.Duration__c}"/>


where ttInstance is the public variable of type Wrapper class TotalTerr



Hope this example helps



Try out the sample code for how to use variables in wrapper class and bound with the variables :


        <apex:repeat value="{!propLstQuesAns}" var="varLQA" id="textquesrp">


        <apex:inputField value="{!varLQA.propAns}" id="textques"/>



         controller code:

                                public List<WrapperQueAns> lstQueAns = new List<WrapperQueAns>{};

        public List<WrapperQueAns> propLstQuesAns { get { return lstQueAns; } set { lstQueAns = value; } }

                                List<Registration_Question__c> lstQues = [select Id, Order__c,Question__c from Registration_Question__c Order by Order__c asc] ;

                                if(lstQues != null && lstQues.size() > 0)


                                                for(Integer i =0; i < lstQues.size(); i++)


                                                                WrapperQueAns wqa = new WrapperQueAns();

                                                                wqa.propQuesId = lstQues[i].Id;

                                                                wqa.propQuesNo = String.valueOf(i + 1) + '. ';

                                                                wqa.propQues   = lstQues[i].Question__c;

                                                                wqa.propAns    = '';


                                                                checkQues = 1;



        // wrapper class:

        public class WrapperQueAns


                                                public String propQuesId{ get; set; }

                                                public String propQuesNo{ get; set; }

                                                public String propQues  { get; set; }

                                                public String propAns   { get; set; }

                                }//End Class WrapperQueAns


Hope this helps.


Thanks for the speedy reply guys. I'm not sure if your solutions will work for me. I apologize for the brevity of my original email, I honestly thought the solution would have been a lot more obvious.


We have two custom objects - Territory and Trade. They are tied through a lookup on Territory Id. I have to create a Visualforce page that will sum up sales by Territory quarterly, yearly and for a date range provided by the end user; along with other date range specific data. The wrapper class is constructed off an AggregateResult:


totalTerr = [SELECT Territory__c territoryId FROM Trade__c WHERE Active__c = True GROUP BY Territory__c ORDER BY Territory__c];


Wrapper class:


public class TotalTerr {

         public Id TerritoryId { get; set; }

	public TotalTerr(AggregateResult ar) {
		TerritoryId = (Id)ar.get('territoryId');

         //YTD Sales Example
	public Double YTDSales {
		get {
			Double defaultValue = 0;
			AggregateResult ytdAR = [SELECT Sum(Principal__c) totalPrincipal FROM Trade__c WHERE Active__c = True AND Trade_Date__c = THIS_FISCAL_YEAR AND Territory__c =: TerritoryId];
			if((Double)ytdAR.get('totalPrincipal') == null) {
				return defaultValue;
			} else {
				return (Double)ytdAR.get('totalPrincipal');


public List<TotalTerr> getTerritoryResults() {
	List<TotalTerr> lstResult = new List<TotalTerr>();
	for(AggregateResult ar: totalTerr) {
		TotalTerr objTotalTerr = new TotalTerr(ar);
	return lstResult;


The issue is when I need to have the following aggregate within the wrapper class:


public Integer ContactedReps {
	get {
		Integer defaultValue = 0;
		Set<Id> contactActivities = new Set<Id>();
		Set<Id> contactSet = new Set<Id>();
		for (Event e : [SELECT WhoId FROM Event WHERE Contact_Date__c >=: startDate AND Contact_Date__c <=: endDate]) {
		for (Task t : [SELECT WhoId FROM Task WHERE Contact_Date__c >=: startDate AND Contact_Date__c <=: endDate]) {
		for (Contact c : [SELECT Id FROM Contact WHERE Id IN : contactSet AND Territory__c =: TerritoryName]) {
		if(contactActivities.size() == 0) {
			return defaultValue;
		} else {
			return contactActivities.size();


I need to be able to retrieve to dates from my form to create a startDate and endDate. I had first thought I could just filter by date in initial totalTerr AggregateResult, but that only would work for my sales totals.  I'm currently trying to send the startDate and endDate through the url by using:


public Date startDate {
	get {
		String sDate = ApexPages.currentPage().getParameters().get('startDate');
		String[] sDates = sDate.split(''); 
		Date startDate = Date.parse(sDate);
		return startDate;

public Date endDate {
	get {
		String eDate = ApexPages.currentPage().getParameters().get('endDate');
		Date endDate = Date.parse(eDate);
		return endDate;


but I'm not sure if I can even use this - hasn't worked yet.



I'm stuck, any help would be greatly appreciated. Thanks in advance.