+ Start a Discussion
james1986james1986 

passing value of SelectList to apex class

hi everyone,

this is really simple.. yet i am lost!

 

I want to pass 2 values from 2 different selectLists in VF to an Apex class. I've found all kinds of stuff online about populating a selectList with data from an Apex class... but nothing about passing a parameter from VF to Apex.

 

My VF code is below. Once I have the 2 variables in Apex, I will use them in my SOQL WHERE clause.

 

thanks in advance!

 

-----------------

<apex:page standardController="Deposit__c" extensions="updatepledges">
<apex:form >  
First Date to include:
<apex:selectList id="first" size="1" >
            <apex:selectoption itemLabel="First of month" itemValue="1"></apex:selectoption>
            <apex:selectoption itemLabel="Second of month" itemValue="2"></apex:selectoption>
            <apex:selectoption itemLabel="Third of month" itemValue="3"></apex:selectoption>
            <apex:selectoption itemLabel="Fourth of month" itemValue="4"></apex:selectoption>
  // ... the list continues like this...
            <apex:selectoption itemLabel="Thirty first of month" itemValue="31"></apex:selectoption>
</apex:selectList>  


Last date to include:
<apex:selectList id="last" size="1" >
            <apex:selectoption itemLabel="First of month" itemValue="1"></apex:selectoption>
            <apex:selectoption itemLabel="Second of month" itemValue="2"></apex:selectoption>
            <apex:selectoption itemLabel="Third of month" itemValue="3"></apex:selectoption>
            <apex:selectoption itemLabel="Fourth of month" itemValue="4"></apex:selectoption>
  // ... the list continues like this...
            <apex:selectoption itemLabel="Thirty first of month" itemValue="31"></apex:selectoption>
</apex:selectList>
<apex:commandButton action="{!runpledges}" value="Submit" />

</apex:form>   
<apex:pageMessages />
</apex:page>

Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

Your selectList is not going to initialize with a value unless you specifically set firstSelect or lastSelect to something in your constructor.  But I think you're going about this the wrong way anyway because your Integer.valueOf() is only going to get called when your class is first created and never when firstSelect or lastSelect are updated.  You'd be better off doing the valueOf conversion on the fly.

 

But all of that being said, I just tried it out and you can definitely make firstSelect and lastSelect an Integer as long as your itemValue is always an integer.  If one of your values isn't an integer (which doesn't seem to be true in your case) your form submission will fail if that value is selected.

All Answers

jwetzlerjwetzler

You have to give your selectList a value attribute that is bound to a String (or in the case of a multi select list, a collection of Strings) property in your controller.  When your form is submitted, that value attribute will be set in your controller.  The value attribute on selectList works the same way it does on any other input component.

james1986james1986

Thanks, Jill. I'm able to save, but when I load the VF page I get this error (even before pressing the button to run the apex class):

 

System.NullPointerException: Argument 1 cannot be null

 Class.updatepledges: line 11, column 41 External entry point  

 

 

Line 11 in my apex class is: "integer firstdate = integer.valueof(firstselect);"

My code is below- any ideas what I'm doing wrong?

 

-------------------------

 VF portion:

<apex:page standardController="Deposit__c" extensions="updatepledges">
<apex:form >  
First Date to include:
<apex:selectList id="first" size="1" value="{!firstselect}" >
            <apex:selectoption itemLabel="First of month" itemValue="1"></apex:selectoption>
            <apex:selectoption itemLabel="Second of month" itemValue="2"></apex:selectoption>
            <apex:selectoption itemLabel="Third of month" itemValue="3"></apex:selectoption>
            <apex:selectoption itemLabel="Fourth of month" itemValue="4"></apex:selectoption>
            <apex:selectoption itemLabel="Fifth of month" itemValue="5"></apex:selectoption>
//...
            <apex:selectoption itemLabel="Thirty first of month" itemValue="31"></apex:selectoption>
</apex:selectList>  
Last date to include:
<apex:selectList id="last" size="1" value="{!lastselect}" >
            <apex:selectoption itemLabel="First of month" itemValue="1"></apex:selectoption>
            <apex:selectoption itemLabel="Second of month" itemValue="2"></apex:selectoption>
            <apex:selectoption itemLabel="Third of month" itemValue="3"></apex:selectoption>
//...
            <apex:selectoption itemLabel="Thirty first of month" itemValue="31"></apex:selectoption>
</apex:selectList>
<apex:commandButton action="{!runpledges}" value="Submit" />

</apex:form>   
<apex:pageMessages />
</apex:page>


------------------

 APEX portion:

public class updatepledges {
 
    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
     // add the instance for the variables being passed by id on the url
    private Deposit__c depbatch {get;set;}
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}
    public String firstselect {get; set;}
    public String lastselect {get; set;}
    integer firstdate = integer.valueof(firstselect);
    integer lastdate = integer.valueof(lastselect);  
      
    public updatepledges(ApexPages.StandardController controller)
    {
        //initialize the stanrdard controller
        this.controller = controller;

        // load the current record
        depbatch = (Deposit__c)controller.getRecord();
}
    public PageReference runpledges()
    {

   
        List<Donation__c> donations = new List<Donation__c>();
        Map<ID,Donation__c> mgDonationMap = new Map<ID,Donation__c>();
        for (Monthly_Gift__c mg: [SELECT Id, Account__c, Contact__c, PaymentType__c FROM Monthly_Gift__c WHERE Day_of_Month__c >= :firstdate AND Day_of_Month__c <= :lastdate])
        {
            //Create a donation and populate it. then add it to the list
            Donation__c d = new Donation__c();
            d.Account__c = mg.Account__c;
            d.Contact__c = mg.Contact__c;
            d.Payment_Type__c = mg.PaymentType__c;
            d.Deposit_Batch__c = depbatch.Id;
            donations.add(d);
            //add the id of the monthly donation and the new dontation to a map to be used for the allocations
            mgDonationMap.put(mg.id,d);
        }
        insert donations;

       
        List<Allocation__c> allocations = new List<Allocation__c>();
        for(Monthly_Gift_Allocations__C mga: [SELECT Amount__c, Monthly_Gift__c, Program__c, Stream__c FROM Monthly_Gift_Allocations__c])
        { 
            Allocation__c a = new Allocation__c();
            a.Amount__c = mga.Amount__c;
            //This is the line that is important.
            //associate the allocation to the correct Donation
            //by finding the monthly gift in the map which the monthly gift allocation is tied to
            a.Transaction__c = mgDonationMap.get(mga.Monthly_Gift__c).Id;
            a.Program__c = mga.Program__c;
            a.Stream__c = mga.Stream__c;
            allocations.add(a);
        }
        insert allocations;
        return new PageReference('/a0a/o');

    }
}

jwetzlerjwetzler

The error message is pretty specific.  You're passing a null value to Integer.valueOf() and that method can't accept a null argument.  Your firstSelect and lastSelect are going to initially be null when your class is initialized.  So you should either default firstSelect and lastSelect to something, or assign those variables using Integer.valueOf in the setters of firstSelect and lastSelect, or you should remove them entirely and do the conversion whenever you need to access firstSelect and lastSelect in your controller.

james1986james1986

OK, that much makes sense. How can I initialize them to the values in the selectlists in the VF page? I though that: <apex:selectList id="last" size="1" value="{!lastselect}" > would initialize lastselect to equal the user's selection.

I am assuming that the Apex Controller does not start running until the user presses submit on the VF page- in which case, I want the variables initalized in VF not Apex. I'm not sure how to initalize the variable to equal the contents of the selectlist.

jwetzlerjwetzler

Your selectList is not going to initialize with a value unless you specifically set firstSelect or lastSelect to something in your constructor.  But I think you're going about this the wrong way anyway because your Integer.valueOf() is only going to get called when your class is first created and never when firstSelect or lastSelect are updated.  You'd be better off doing the valueOf conversion on the fly.

 

But all of that being said, I just tried it out and you can definitely make firstSelect and lastSelect an Integer as long as your itemValue is always an integer.  If one of your values isn't an integer (which doesn't seem to be true in your case) your form submission will fail if that value is selected.

This was selected as the best answer
james1986james1986

thanks Jill! I got it working with the code below.

 

In this case, updating the records after the fact is not a requirement - only selecting records within a range and adding it to a custom object to avoid data entry.

 

 

--------------------------------------------------

 

public class updatepledges {
 
    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
     // add the instance for the variables being passed by id on the url
    private Deposit__c depbatch {get;set;}
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}
    public String firstselect {get; set;}
    public String lastselect {get; set;}
 
      
    public updatepledges(ApexPages.StandardController controller)
    {
        //initialize the stanrdard controller
        this.controller = controller;

        // load the current record
        depbatch = (Deposit__c)controller.getRecord();
}
    public PageReference runpledges()
    {

        integer firstdate = integer.valueof(firstselect);
    integer lastdate = integer.valueof(lastselect);
        List<Donation__c> donations = new List<Donation__c>();
        Map<ID,Donation__c> mgDonationMap = new Map<ID,Donation__c>();
        for (Monthly_Gift__c mg: [SELECT Id, Account__c, Contact__c, PaymentType__c FROM Monthly_Gift__c WHERE Day_of_Month__c >= :firstdate AND Day_of_Month__c <= :lastdate])
        {
            //Create a donation and populate it. then add it to the list
            Donation__c d = new Donation__c();
            d.Account__c = mg.Account__c;
            d.Contact__c = mg.Contact__c;
            d.Payment_Type__c = mg.PaymentType__c;
            d.Deposit_Batch__c = depbatch.Id;
            donations.add(d);
            //add the id of the monthly donation and the new dontation to a map to be used for the allocations
            mgDonationMap.put(mg.id,d);
        }
        insert donations;

       
       
        List<Allocation__c> allocations = new List<Allocation__c>();
        for(Monthly_Gift_Allocations__C mga: [SELECT Amount__c, Monthly_Gift__c, Program__c, Stream__c FROM Monthly_Gift_Allocations__c WHERE Monthly_Gift__r.Day_of_Month__c >= :firstdate AND Monthly_Gift__r.Day_of_Month__c <= :lastdate])
        { 
            Allocation__c a = new Allocation__c();
            a.Amount__c = mga.Amount__c;
            //This is the line that is important.
            //associate the allocation to the correct Donation
            //by finding the monthly gift in the map which the monthly gift allocation is tied to
            a.Transaction__c = mgDonationMap.get(mga.Monthly_Gift__c).Id;
            a.Program__c = mga.Program__c;
            a.Stream__c = mga.Stream__c;
            allocations.add(a);
        }
        insert allocations;
        return new PageReference('/a0a/o');

    }
}

 

------------------------------

 

tandardController="Deposit__c" extensions="updatepledges">
<apex:form >  
First Date to include:
<apex:selectList id="first" size="1" value="{!firstselect}" >
            <apex:selectoption itemLabel="First of month" itemValue="1"></apex:selectoption>
            <apex:selectoption itemLabel="Second of month" itemValue="2"></apex:selectoption>
            <apex:selectoption itemLabel="Third of month" itemValue="3"></apex:selectoption>
            <apex:selectoption itemLabel="Fourth of month" itemValue="4"></apex:selectoption>
            <apex:selectoption itemLabel="Fifth of month" itemValue="5"></apex:selectoption>
            <apex:selectoption itemLabel="Sixth of month" itemValue="6"></apex:selectoption>
            <apex:selectoption itemLabel="Seventh of month" itemValue="7"></apex:selectoption>
            <apex:selectoption itemLabel="Eight of month" itemValue="8"></apex:selectoption>
            <apex:selectoption itemLabel="Ninth of month" itemValue="9"></apex:selectoption>
            <apex:selectoption itemLabel="Tenth of month" itemValue="10"></apex:selectoption>
            <apex:selectoption itemLabel="Eleventh of month" itemValue="11"></apex:selectoption>
            <apex:selectoption itemLabel="Twelfth of month" itemValue="12"></apex:selectoption>
            <apex:selectoption itemLabel="Thirteenth of month" itemValue="13"></apex:selectoption>
            <apex:selectoption itemLabel="Fourteenth of month" itemValue="14"></apex:selectoption>
            <apex:selectoption itemLabel="Fifteenth of month" itemValue="15"></apex:selectoption>
            <apex:selectoption itemLabel="Sixteenth of month" itemValue="16"></apex:selectoption>
            <apex:selectoption itemLabel="Seventeenth of month" itemValue="17"></apex:selectoption>
            <apex:selectoption itemLabel="Eighteenth of month" itemValue="18"></apex:selectoption>
            <apex:selectoption itemLabel="Ninteenth of month" itemValue="19"></apex:selectoption>
            <apex:selectoption itemLabel="Twentieth of month" itemValue="20"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty first of month" itemValue="21"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty second of month" itemValue="22"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty third of month" itemValue="23"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty fourth of month" itemValue="24"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty fifth of month" itemValue="25"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty sixth of month" itemValue="26"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty seventh of month" itemValue="27"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty eight of month" itemValue="28"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty ninth of month" itemValue="29"></apex:selectoption>
            <apex:selectoption itemLabel="Thirtieth of month" itemValue="30"></apex:selectoption>
            <apex:selectoption itemLabel="Thirty first of month" itemValue="31"></apex:selectoption>
</apex:selectList>  
Last date to include:
<apex:selectList id="last" size="1" value="{!lastselect}" >
            <apex:selectoption itemLabel="First of month" itemValue="1"></apex:selectoption>
            <apex:selectoption itemLabel="Second of month" itemValue="2"></apex:selectoption>
            <apex:selectoption itemLabel="Third of month" itemValue="3"></apex:selectoption>
            <apex:selectoption itemLabel="Fourth of month" itemValue="4"></apex:selectoption>
            <apex:selectoption itemLabel="Fifth of month" itemValue="5"></apex:selectoption>
            <apex:selectoption itemLabel="Sixth of month" itemValue="6"></apex:selectoption>
            <apex:selectoption itemLabel="Seventh of month" itemValue="7"></apex:selectoption>
            <apex:selectoption itemLabel="Eight of month" itemValue="8"></apex:selectoption>
            <apex:selectoption itemLabel="Ninth of month" itemValue="9"></apex:selectoption>
            <apex:selectoption itemLabel="Tenth of month" itemValue="10"></apex:selectoption>
            <apex:selectoption itemLabel="Eleventh of month" itemValue="11"></apex:selectoption>
            <apex:selectoption itemLabel="Twelfth of month" itemValue="12"></apex:selectoption>
            <apex:selectoption itemLabel="Thirteenth of month" itemValue="13"></apex:selectoption>
            <apex:selectoption itemLabel="Fourteenth of month" itemValue="14"></apex:selectoption>
            <apex:selectoption itemLabel="Fifteenth of month" itemValue="15"></apex:selectoption>
            <apex:selectoption itemLabel="Sixteenth of month" itemValue="16"></apex:selectoption>
            <apex:selectoption itemLabel="Seventeenth of month" itemValue="17"></apex:selectoption>
            <apex:selectoption itemLabel="Eighteenth of month" itemValue="18"></apex:selectoption>
            <apex:selectoption itemLabel="Ninteenth of month" itemValue="19"></apex:selectoption>
            <apex:selectoption itemLabel="Twentieth of month" itemValue="20"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty first of month" itemValue="21"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty second of month" itemValue="22"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty third of month" itemValue="23"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty fourth of month" itemValue="24"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty fifth of month" itemValue="25"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty sixth of month" itemValue="26"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty seventh of month" itemValue="27"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty eight of month" itemValue="28"></apex:selectoption>
            <apex:selectoption itemLabel="Twenty ninth of month" itemValue="29"></apex:selectoption>
            <apex:selectoption itemLabel="Thirtieth of month" itemValue="30"></apex:selectoption>
            <apex:selectoption itemLabel="Thirty first of month" itemValue="31"></apex:selectoption>
</apex:selectList>
<apex:commandButton action="{!runpledges}" value="Submit">
    <apex:param assignTo="{!lastselect}" value="{!lastselect}" name="lastselect"/>
    <apex:param assignTo="{!firstselect}" value="{!firstselect}" name="firstselect"/>
</apex:commandButton>
</apex:form>   
<apex:pageMessages />