Activity Forums Salesforce® Discussions How can I create custom picklist on a Salesforce Visualforce Page?

  • Adarsh

    Member
    March 22, 2018 at 12:51 pm

    Hi Ankit,

    you may go through with this sample of code,

    <apex:page controller="TestController ">
    <apex:form >
    <apex:selectList size="1" value="{!selectedname}">
    <apex:selectOptions value="{!selectedaccnamefields}"/>
    </apex:selectList>
    </apex:form>
    </apex:page>

    public class TestController {
    Public string selectedname{get;set;}
    Public List<Selectoption> getselectedaccnamefields(){
    List<Selectoption> lstnamesel = new List<selectoption>();
    lstnamesel.add(new selectOption('', '- None -'));
    for(Account acc :[SELECT id,name,phone,type,industry FROM Account]){
    lstnamesel.add(new selectoption(acc.id,acc.name));
    }
    return lstnamesel;
    }
    }

    Hope it helps 🙂

  • PRANAV

    Member
    March 23, 2018 at 11:04 am

    Hi Ankit,

    VF Page

    <apex:page tabStyle="Case" controller="customPicklist">
    <apex:form >
    <apex:pageBlock title="Custom PickList Demo" id="out">
    <apex:pageBlockButtons >
    <apex:commandButton value="Save" action="{!save}" rerender="out" status="actStatusId"/>
    <apex:actionStatus id="actStatusId">
    <apex:facet name="start">
    <img src="/img/loading.gif" />
    </apex:facet>
    </apex:actionStatus>
    </apex:pageBlockButtons>
    <apex:pageBlockSection title="Custom Picklist Using selectList and selectOptions">
    <apex:selectList value="{!selectedCountry2}" multiselect="false" size="1">
    <apex:selectOptions value="{!countriesOptions}"/>
    </apex:selectList>
    <apex:outputText value="{!selectedCountry2}" label="You have selected:"/>
    </apex:pageBlockSection>
    </apex:pageBlock>
    <apex:pageBlock >

    </apex:pageBlock>
    </apex:form>
    </apex:page>

    -------------------------------------------------------------

    Apex Controller Class

    public with sharing class customPicklist {
    public String selectedCountry2{get;set;}
    public List<String> selectedCategories { get; set; }

    public List<SelectOption> getCountriesOptions() {
    List<SelectOption> countryOptions = new List<SelectOption>();
    countryOptions.add(new SelectOption('','-None-'));
    countryOptions.add(new SelectOption('INDIA','India'));
    countryOptions.add(new SelectOption('USA','USA'));
    countryOptions.add(new SelectOption('United Kingdom','UK'));
    countryOptions.add(new SelectOption('Germany','Germany'));
    countryOptions.add(new SelectOption('Ireland','Ireland'));

    return countryOptions;
    }

     

    public List<SelectOption> getCategories() {

    List<SelectOption> categories = new List<SelectOption>();

    for(Case c : [Select sahi__MultiSelectPicklist__c from Case limit 10])

    categories.add(new SelectOption(c.sahi__MultiSelectPicklist__c, c.sahi__MultiSelectPicklist__c));

    return categories;

    }
    public PageReference save(){
    return null;
    }

    }

    Hope this helps you.

  • Archit

    Member
    March 23, 2018 at 12:25 pm

    Visualforce Page

    <apex:page controller="DisplayData">
    <apex:form >
    <apex:pageBlock title="Search Here">
    <b>Object : </b>
    <apex:selectList size="1" value="{!selectObject}">
    <apex:selectOption itemValue="None" itemLabel="-None-"/>
    <apex:selectOption itemValue="Account" itemLabel="Account"/>
    <apex:selectOption itemValue="Contact" itemLabel="Contact"/>
    </apex:selectList>
    <apex:commandButton value="Display" action="{!SearchObject}"/>
    </apex:pageBlock>
    <apex:pageBlock title="Search Result">
    <apex:pageBlockTable value="{!resultList}" var="res">
    <apex:column value="{!res['Name']}"/>
    </apex:pageBlockTable>
    </apex:pageBlock>
    </apex:form>
    </apex:page>

    Apex Class

    public class DisplayData {

    Public list<Sobject> resultList{get;set;}
    public string selectObject{get;set;}

    public DisplayData()
    {
    resultList = new list<Sobject>();
    }

    public void SearchObject()
    {
    if(selectObject != 'None')
    resultList = Database.query('SELECT Name FROM '+selectObject+' LIMIT 20');

    }
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos