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
Allen ManamelAllen Manamel 

map dynamic picklist chosen value from visualforce to an actual field after retrieving id

I have a dynamic picklist that displays the name of the nominator record + contact name field on that record. What I want is that when the user selects a value from the picklist it should fetch the id of the nominator record and save it in Congressional District field(nominator lookup field) on the application object in the saveandcontinue function
Note: Nominator is my custom object, contact is a field on nominator record

Here is my visualforce code

     <apex:selectList size="1" id="a">
         <apex:selectOptions value="{!contactlist}"></apex:selectOptions>
     </apex:selectList>

Here is my apex class

          public List<Nominator__c> Nominators = new List<Nominator__c>(); 
       
           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 =: contact.HS_Grad_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
        
          {
        
        
            
        
          }
    } 

public pagereference saveAndContinue() {
// Need to write the code here
update application;
Pagereference Page = new Pagereference('/apex/Page_4');
Page.setRedirect(true);
return Page;
}


I know that we can save a user selected value on visualforce by using the value field on the selectlist but the part where I am stuck at is that how can I find the id of the nominator record chosen by the user and then save it in a field. Right now the select list only has the nominator name + contact data displayed on the picklist. Do I have to run a SOQL query again in the set function to grab the id or is there any other way? 
Best Answer chosen by Allen Manamel
Allen ManamelAllen Manamel

I solved it by changing the selectList statement to the following

<apex:selectList value="{!SelectedNomination}" size="1" id="a">

Added a new Id variable in my apex class

public Id SelectedNomination { get; set;} 

Added the following line in my saveandcontinue function

application.Congressional_District__c = SelectedNomination;