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
ezdhanhussainezdhanhussain 

add new values in sobject picklist.

Hi i have an sobject in which a picklist with 3 values has been saved. Now i want to give additional values in picklist via a input text box in  visualforce and apex. I am able to fetch the records here. but unable to add values to it.  Adding my code for reference.

<apex:page controller="clientobject">
<apex:form >
 <apex:pageBlock >
  <apex:selectList id="countries" value="{!picklist}" size="1">
  <apex:selectOptions value="{!countries}"/>
</apex:selectList><br/><br/>
  Enter value to add:<apex:inputText value="{!pick}"/>
  <apex:commandButton value="add" />
 </apex:pageBlock>
</apex:form>
</apex:page>
//controller class
public class clientobject {
public string pick{set;get;}
public string picklist{set;get;}
transient List<Schema.PicklistEntry> ple = new List<Schema.PicklistEntry>();
public clientobject(){

}
public List<SelectOption> getCountries(){
  List<SelectOption> options = new List<SelectOption>();
        
   Schema.DescribeFieldResult fieldResult = OfficeLocation__c.Country__c.getDescribe();
  ple = fieldResult.getPicklistValues();
    
        
   for( Schema.PicklistEntry f : ple)
   {
 
      options.add(new SelectOption(f.getLabel(), f.getValue()));
      
   }       
    system.debug('picklist values are ::::::::::'+options);
   return options;
  }
 }

 i don't know how to perform dml actions on schema.describe calls ? Any suggestions would be appreciated

Arunkumar.RArunkumar.R

You cannot add picklist values through apex class or visualforce force to sobject... 

 

The only way go for metadata api class...

 

This one is the solution for adding picklist field and their values for sobject .....

 

you can find out the metadata api class and their services from the below,

 

https://github.com/financialforcedev/apex-mdapi

 

 

If you found this answer helpful to you... Please accept this as a Solution and  give kudos by clicking on the star icon.

 

Thanks and Regards,

Arunkumar.R | Salesforce Certified Developer

Satish_SFDCSatish_SFDC
Yes, Picklist values can only be added either through the UI or through the Metadata api.

Regards,
Satish Kumar
GlynAGlynA

Hussain,

 

You could do something like this using Custom Settings instead of a picklist.  Your page would display all the values in your "Countries" custom setting, and allow the user to add new values as new custom settings.

 

The Country__c field of the OfficeLocation__c object would just be a text field.

 

You could add a VisualForce page as an element in the standard detail page layout of the OfficeLocation__c object to allow your users to view/edit the country value.  The Country__c field would not appear on the edit layout.

 

If you have any questions about this idea, let me know - I will be happy to help you with it.

 

-Glyn