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>

Here is my apex class

public List<Nominator__c> Nominators = new List<Nominator__c>();    
public List<SelectOption> contactlist



        String state =               
        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.Name, nom.Name + ' ' + 


        return contactlist;





    public pagereference saveAndContinue() {

      // Need to write the code here
      update application;
      Pagereference Page = new Pagereference('/apex/Page_4');
      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?

1 Answer 1


Instead of providing the name of the Nominator, provide the id.

contactlist.add(new SelectOption(nom.Id, nom.Name + ' ' + nom.Contact_Name__c));

You need a variable to store your selected value in. This means a variable and a small change to your select list:

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

You're trying to use the set method of the list, but this list won't know which record was selected, thats why we need the extra variable to track the selected value. You can rig the setter to change the value on the record as well:

public Id SelectedNomination { 
    set { 
        // value is a provided variable with whatever this variable is being set to 
        SelectedNomination = value; 
        application.Congressional_District__c = value; 

I'm unsure of how well using a set to set this variable will be, I'd probably add a line to your save method to carry over the value there, instead of in the setter.

You could also use an actionFunction in conjunction with an actionRegion to submit the field's value to the controller to perform additional processing, but that kind of heavy lifting is probably better suited for code which needs to rerender components to display changed field values.

Also, you should use lazy-loading when creating a list of selectOptions (really anything you need to query to build).

get {
    if (contactList == null) {
        contactlist = new List<SelectOption>();
        // .. add values, run query .. 

    return contactList; 
  • Thanks for the help. I am not sure if I understood the solution completely. I changed this line contactlist.add(new SelectOption(nom.Id, nom.Name + ' ' + nom.Contact_Name__c)); and my bad I actually have to do this in saveandcontinue function. I have updated the description in my question above. And the name of the value I am using on my visualforce is contactList which is used to display the picklist values. I am not sure how can I use a separate variable to store the value selected by the user ?
    – Student
    Commented Apr 9, 2018 at 20:27
  • The variable in the value attribute is used to store the chosen value from the select options. The value is determined by the first parameter of the SelectOption constructor, named "value". The second parameter is the one shown to the end user. Apex will automatically set this variable once the form is submitted to the value of the chosen select option, allowing you to reference it in your save method. Pretty much all you need to do is application.Congressional_District__c = SelectedNomination in your save method. Commented Apr 9, 2018 at 20:38
  • Probably also want to check for save failures, display any failure messages & conditionally change your return value from your save method, but now Im getting out of the scope of the question. Commented Apr 9, 2018 at 20:39
  • Yippeee it worked!! So I changed the selectList statement as specified by you. Added this in apex class public Id SelectedNomination { get; set;} And added the following line in my save function application.Congressional_District__c = SelectedNomination; It worked like a charm. Thank you so much!!
    – Student
    Commented Apr 9, 2018 at 21:38
  • Also I have another query. Once the user selected option is saved in the actual field. When I open the visualforce page again, it should display the value user selected last time instead of some random value in picklist field.
    – Student
    Commented Apr 10, 2018 at 14:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .