0

I have a requirement where I am not displaying the actual salesforce picklist field on visualforce page but rather a self created selectlist with some values. I was able to display it on visualforce page but don't know how to fetch the user selected value and store it into another field on apex class.

Here is my visualforce code to display the picklist:

<apex:selectList value="{!AlcoholInvolvedId}" style="width:40%;" size="1"> 

          <apex:selectOptions value="{!AlcoholInvolved}"/>  

</apex:selectList>  

<apex:commandButton id="Save" value="Save" action="{!saveAndContinue}"

Here is my controller class:

public String AlcoholInvolvedId { get;set; }
    public List<SelectOption> AlcoholInvolved{

      get

         {

            //Alcohol_Involved = new List<SelectOption>();
            List<Selectoption> Alcoholinv = new List<selectoption>();
            Alcoholinv.add(new selectOption('', '--None--'));
            Alcoholinv.add(new selectoption('', 'Yes'));
            Alcoholinv.add(new selectoption('', 'No'));

            return Alcoholinv; 

         }

       set {}


  }


 public pagereference saveAndContinue() {


    Application__c app= new Application__c(

    .........,
    Alcohol_Involved__c= AlcoholInvolvedId,
    );

      insert app;


      ............
      .............

  }

Here is my picklist definiton on the backend. enter image description here

2

You're setting the "value" to nothing in all the options. You need to specify a value:

    Alcoholinv.add(new selectOption('', '--None--'));
    Alcoholinv.add(new selectoption('Yes', 'Yes'));
    Alcoholinv.add(new selectoption('No', 'No'));
  • Hi, Thanks for replying. I changed it to Alcoholinv.add(new selectOption('', '--None--')); Alcoholinv.add(new selectoption('Y', 'Yes')); Alcoholinv.add(new selectoption('N', 'No')); I am getting this error on visualforce now. It does not let me save the page. Error: id Y must be 15 characters – Student Aug 2 '18 at 21:58
  • If it's an actual ID value, that means you're going to have to write a query to get the correct values. You can't just write 'Y' or 'N'. See this answer for how to do that. – sfdcfox Aug 2 '18 at 22:09
  • I don't know what the id would be in this case. It is a picklist in Salesforce with the global picklist values Yes and No and API names Y and N respectively. – Student Aug 2 '18 at 22:31
  • @Student I'm pretty sure you're using the wrong field. You wouldn't get that error if it were a global picklist. Check the Alcohol_Involved__c field, I'm pretty sure it's a lookup for whatever reason. – sfdcfox Aug 2 '18 at 22:33
  • I am not sure if global value set and global picklist are the same things or different. I have attached the screenshot of the picklist definition above. – Student Aug 2 '18 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.