You need to sign in to do that
Don't have an account?
Kevin Kim 9
Dynamically adding picklist values
I want to let users add values to the picklist if it is not already on there by choosing an "other" option. New to VF and Apex and need help.
function readOnly(count){ }
You need to sign in to do that
Don't have an account?
Scenario..
Problem explanation with Example: A picklist field "City" with values: Delhi, Mumbai, Chennai, Kolkata(4 picklist values) and a textbox nearby to enter any other city. Supposing if Bangalore has to be entered, the user has to use the textbox. Now next time when a record is created, the "City" field should show "Bangalore" as 5th value.
Screenshots:
As you can see below, there are only three values available for selection.
I am selecting 'Other' from the drop-down, this action displays a text box for the new value and a button called "Add".
I am entering a new value called "Bangalore" and I am clicking on "Add"
Done!! Now you can see that the new value "Bangalore" is also added to the drop-down.
We will achieve this functionality using visualforce. You can then extend the code provided here to as many Visualforce pages as you want.
Step 1:
First of all, we will have to create an object to hold all the picklist values. So, go ahead and create the following.
Object name: DynamicPicklist
Field Names:
Name (Datatype: Autonumber)
PicklistValue (Datatype: text)
Step 2:
Add picklist values to the object created.. (ie) Add records to the newly created object...
For example..
Record 1: PicklistValue= 'Delhi'
Record 2: PicklistValue = 'Mumbai'
and so on...........
Now also add a value called '--Other--'. This value will be used for the user to enter a new value.
Step 3:
Creation of the Visualforce page and the Apex Class.
Apex Code.. Save this first Visual Force Code: How to extend this code???
Are you wondering if you will have to create one object for one picklist and suppose you have ten picklists like these, then ten objects... That is really a mess...
So, why not create ten fields in the same object.. Field1 represents picklist1, field2 picklist2 and so on...
But, be clear on the way you retrieve values and insert values into this object... For example, I have not performed the validation to check if the newly entered value already exists.
Hope this helps.
Kindly mark this as solved if the information was helpful so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.
Thanks,
Nagendra