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
SFDC16SFDC16 

How insert value for custom pick-list value from vf page?

Hello Developer,

I am trying to insert a new record from vf page I have some custom fields
but I don't know exactly how to insert picklist value from vf page.



Note: State is custom filed which contains all Indian state.

Below is my vf and apex code.


                                <apex:pageBlock id="insertMaserData">

                                            <div class="slds-align_absolute-center" style="height: 3rem;">

                                            </div>
                                            <table class='table'>

                                                <tr>
                                                    <td><b>Name</b></td>
                                                    <td><apex:inputtext value="{!customerName}" label="Customer Name" styleClass="slds-input"  style="width:150px" required="true"/> </td>
                                                    <td><b>Contact Number</b></td>
                                                    <td><b><apex:inputtext value="{!contactNumber}" label="Customer Name" styleClass="slds-input"  style="width:150px"  required="true"/> </b></td>
                                               </tr>
                                                
                                                <tr>
                                                    <td><b>Customer Email</b></td>
                                                    <td><apex:inputtext value="{!customerEmail}" label="Customer Name" styleClass="slds-input"  style="width:150px" required="true"/> </td>
                                                    <td><b>Customer Address</b></td>
                                                    <td><b><apex:inputtext value="{!customerAddres}" label="Customer Name" styleClass="slds-input"  style="width:150px"  required="true"/> </b></td>
                                               </tr>
                                                
                                                <tr>
                                                    <td><b>Customer Age</b></td>
                                                    <td><apex:inputtext value="{!customerAge}" label="Customer Name" styleClass="slds-input"  style="width:150px" required="true"/> </td>
                                                    <td><b>PAN Number</b></td>
                                                    <td><b><apex:inputtext value="{!panNumber}" label="Customer Name" styleClass="slds-input"  style="width:150px"  required="true"/> </b></td>
                                                </tr>
                                                
                                                <tr>
                                                   
                                                    <td><b>State Name</b></td>
                                                    <td><apex:inputtext value="{!stateName}" label="Customer Name" styleClass="slds-input"  style="width:150px" required="true"/> </td>
                                              
                                                </tr>

                                               
                                            </table>
                                            <div class="slds-align_absolute-center" style="height: 3rem;">

                                                <apex:commandButton action="{!insertMasterRecord}" value="Save" styleClass="slds-button slds-button_neutral" oncomplete="alert('Record Inserted SuccessFully..!!');" />
                                                <button type="button" class="slds-button slds-button_neutral" data-dismiss="modal">Close</button>

                                            </div>

                                        
                                </apex:pageBlock>

=======Apex code===============

public class InsertMasterData {
 public String customerName{get;set;}
    public Integer contactNumber{get;set;}
    public String customerAddres{get;set;}
    public String customerEmail{get;set;}
    public Integer customerID{get;set;}
    public String panNumber{get;set;}
    public String stateName{get;set;}
    public String customerAge{get;set;}
 
    public InsertMasterData()
    {

        
    }
    

   public PageReference  insertMasterRecord()
    {
        
        Customer_Master__c cm=new Customer_Master__c();
        cm.Name=customerName;
        cm.Contact_Number__c=contactNumber;
        cm.Customer_Address__c=customerAddres;       
        cm.Customer_Email__c=customerEmail;
        cm.Pan_Number__c=panNumber;
        cm.State__c=stateName; // picklist value
        cm.Age__c=customerAge;
        
        insert cm;
}
Best Answer chosen by SFDC16
Ramesh DRamesh D
You can do this using MetaDataAPI. In MetadataAPI you can add picklist Value
Go thorugh below links
https://www.ajaydubedi.com/featured/salesforce-metadata-api/
https://developer.salesforce.com/forums/?id=906F000000092beIAA

Thanks
Ramesh
 

All Answers

Ramesh DRamesh D
You can do this using MetaDataAPI. In MetadataAPI you can add picklist Value
Go thorugh below links
https://www.ajaydubedi.com/featured/salesforce-metadata-api/
https://developer.salesforce.com/forums/?id=906F000000092beIAA

Thanks
Ramesh
 
This was selected as the best answer
Deepali KulshresthaDeepali Kulshrestha
Hi SFDC16,
According to your requirement, you have to insert a value to the picklist through a custom field. I have done the same in 

the following code. You can make a query on the object on which you are using it and then you can pass the list to the 

picklist. I have done it in the Contact object.It will add the new contact that has been created in the picklist. 

picklist 
Please refer to the following code as it may be helpful in solving your problem:
 
VF Page:
<apex:page controller="dynamiccontactlist">
    <apex:form >
      <apex:selectList size="3">
          <apex:selectOptions value="{!contactlist}"></apex:selectOptions>
      </apex:selectList>
  </apex:form>
 
</apex:page>

Controller:
public with sharing class dynamiccontactlist {
    public List<contact> ContactTemp = new List<Contact>();
    public dynamiccontactlist()
    {
     
    }
     
    public List<SelectOption> contactlist
    {
        get
        {
            ContactTemp = [Select firstname,lastname from contact];
             
            contactlist = new List<SelectOption>();
             
            for(contact con : ContactTemp)
            {
                contactlist.add(new SelectOption(con.lastname, con.lastname));
            }
            return contactlist;
        }
        set;
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
SFDC16SFDC16
Hello Deepali, I tried with ur code but in the state__C field, I am getting saved record values please see in below screen. It's not showing exact picklist values i.e all-state. [image: image.png]