I have a dynamic picklist that I am displaying on the visualforce page.
Here is my visualforce code:
<apex:selectList value="{!SelectedNomination}" size="1" id="a">
<apex:selectOptions value="{!contactlist}"></apex:selectOptions>
</apex:selectList>
Here is my apex code:
public Id SelectedNomination { get; set;}
public List<Nominator__c> Nominators = new List<Nominator__c>();
// The below code is in my class constructor method.
public List<SelectOption> contactlist
{
get
{
String state = application.Congressional_State_Territory__c.substring(0,2);
Nominators = [Select Id, Name, Class_Year__c, Contact_Name__c,
Nominator_Code__c, Nominator_Type__c from Nominator__c WHERE
FirstTwoLetters__c =: state and Nominator_Type__c=
'Congressional - House' and Class_Year__c =:
application.Class_Year__c and Status__c =:'Active' ];
contactlist = new List<SelectOption>();
for(Nominator__c nom : Nominators)
{
contactlist.add(new SelectOption(nom.Id, nom.Name + ' ' +
nom.Contact_Name__c));
}
return contactlist;
}
set {}
}
I am saving the user chosen value in SelectedNomination variable and I am saving the value in congressional district field in the saveandcontinue fucntion
public pagereference saveAndContinue() {
application.Congressional_District__c = SelectedNomination;
update application;
Pagereference Page = new Pagereference('/apex/usafa_PCQ_Page_4');
Page.setRedirect(true);
return Page;
}
Test class:
I have started writing the code but it's in pieces and I am stuck.
Nominator__c Nominators = [Select Id, Name, Class_Year__c, Contact_Name__c, Nominator_Code__c, Nominator_Type__c from Nominator__c
WHERE FirstTwoLetters__c =: 'GA' and Nominator_Type__c= 'Congressional - House' and Class_Year__c =: '2023' and Status__c =:'Active' ];
contactlist = new List<SelectOption>();
for(Nominator__c nom : Nominators)
{
contactlist.add(new SelectOption(nom.Id, nom.Name + ' ' +
nom.Contact_Name__c));
}
//u is my current user
system.runAs(u){
usafa_PCQPage3Controller controllerClass = new usafa_PCQPage3Controller();
List<SelectOption> selOpts=controllerClass.contactlist;
}
set {}
maybe cause you some grief, a custom setter needs to at least look likeset { contactlist = value; }
or it won't actually set the value. A default setter just looks likeset;
without the {}.