Reading list Switch to dark mode

    How to create Dynamic Drop-Down in Visualforce Page

    Updated 16 July 2021

    In this post, I am going to share the information How to create Dynamic Drop-Down in Visualforce Page.

    apex:selectlist  & apex:selectoption

    Gratefully, there is a way to effectively create a Dynamic Drop-Down in Visualforce Page with list of standard & custom object records based on a query.

    <apex:selectlist> : A list of options that allows users to select only one value or multiple values at a time, depending on the value of its multiselect attribute.

    <apex:selectoption> : The component must be a child of . To bind your options from Apex to your visualforce using apex:selectOptions you need to use the SelectOption object type in Apex.

    Below is a snapshot of the code from the apex controller, and also the Visualforce page.

    Searching for an experienced
    Salesforce Company ?
    Find out More

    Apex:

    public with sharing class dynamicpicklist {
    
      
            /**
             * Webkul Software.
             *
             * @category  Webkul
             * @author    Webkul
             * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
             * @license   https://store.webkul.com/license.html
             */
     public dynamicpicklist()
        {
        
        }
    
        public List<SelectOption> getdynamiclist() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('INDIA','INDIA'));
            options.add(new SelectOption('US','US'));
            options.add(new SelectOption('BRAZIL','BRAZIL'));
            return options;
        }
    
    }
    

    Visualforce Page :

    <apex:page controller="dynamicpicklist">
    
       <!-- 
            /**
             * Webkul Software.
             *
             * @category  Webkul
             * @author    Webkul
             * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
             * @license   https://store.webkul.com/license.html
             */
             -->
    
      <apex:form >
          <apex:selectList size="1">
              <apex:selectOptions value="{!dynamiclist}"></apex:selectOptions>
          </apex:selectList>
      </apex:form>
    
    </apex:page>
    
    

    Output
    dynamic datapicklist

    if size = 2

    sizevalue

    Record type dropdown

    Apex:

    public with sharing class dynamiccontactlist {
    
            /**
             * Webkul Software.
             *
             * @category  Webkul
             * @author    Webkul
             * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
             * @license   https://store.webkul.com/license.html
             */
    
    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;
        }
    }

    Visualforce Page :

    <apex:page controller="dynamiccontactlist">
    
     <!-- 
            /**
             * Webkul Software.
             *
             * @category  Webkul
             * @author    Webkul
             * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
             * @license   https://store.webkul.com/license.html
             */
             -->
    
      <apex:form >
          <apex:selectList size="3">
              <apex:selectOptions value="{!contactlist}"></apex:selectOptions>
          </apex:selectList>
      </apex:form>
    
    </apex:page>

    Output

    HERE SIZE = 3

    CONTACTLIST

    Support

    That’s all for How to create Dynamic Drop-Down in Visualforce Page in Salesforce, still have any issue feel free to add a ticket and let us know your views to make the product better http://webkul.com/ticket/index.php

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    1 comments

  • Umesh B.P
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content