Home > Blog > How to Create Dynamic custom Dependent Picklist On any object Within Salesforce

How to Create Dynamic custom Dependent Picklist On any object Within Salesforce

  The requirement is to get all the State values based on the Country values from the standard Country to State picklist values. In many cases, we may need to query all the States active for a specific country. The dependent picklist salesforce is very useful within the Salesforce CRM for many reasons.  It is used to limit the list of values in one picklist based on the value of another picklist. An example would be if you had a list of continents, countries, states, regions and major cities.  If you select a Continent as an example, you would want the country to be filtered in the next picklist within Salesforce.  The country selected should then limit the states that would be available for selection and so on. I.e. for country United States the values returned should be “Alabama”, “Alaska”, “Arizona”,……etc. Importance: 1. In a custom page, we want to show user’s valid States to choose from based on the Country chosen by the user. 2. We might have an external UI application using the Force.com platform and we need to expose State values based on Country values. To do so, I need to fetch dependent picklist values based on the controlling field value. Setup: If you follow these simple steps within Salesforce, you can create a dynamic dependent picklist: Step-1- First, define your Visulforce Page with the picklists:
  • Go To Setup a Develop a Pages
  • Click the “New” button for creating new Visualforce pages within Salesforce
    In the Visualforce page editor paste this following below code. You can see that it uses a custom controller and an event “onchange” on country list to change the content of the states list when you select a country. It looks like this : <apex:page id=”page” controller=”dependentPickLst”> <apex:form id=”idForm”> <apex:pageBlock > <apex:pageBlockSection columns=”2″> <apex:outputText >Country:</apex:outputText> <apex:pageBlockSectionItem id=”idFirst”> <apex:selectList size=”1″ value=”{!strCountryName}” title=”Country”> <apex:selectOptions value=”{!Countries}”></apex:selectOptions> <apex:actionSupport event=”onchange” reRender=”idForm”/> </apex:selectList> </apex:pageBlockSectionItem> </apex:pageBlockSection> <apex:pageBlockSection columns=”2″> <apex:outputText >States:</apex:outputText> <apex:pageBlockSectionItem id=”idDepend”> <apex:selectList size=”1″ value=”{!strStateName}”> <apex:selectOptionsvalue=”{!States}”> </apex:selectOptions> </apex:selectList> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>  
  • “Save” this code.
Step-2- Then define the APEX Class dependentPickLst  to populate the options depending on the selected country:
  • Go To Setup a Develop an Apex Classes
  • Click the “New” button for creating new Apex class:
  • Copy this code and paste it in your editor
  public class dependentPickLst { public String strCountryName { get; set; } public String strStateName { get; set; }   public List<SelectOption> getCountries() { List<SelectOption> lstOption = new List<SelectOption>(); lstOption.add(new SelectOption(‘None’, ‘– Select –‘)); lstOption.add(new SelectOption(‘IN’, ‘India’)); lstOption.add(new SelectOption(‘US’, ‘United Sate’));   return lstOption; }   public List<SelectOption> getStates() { List<SelectOption> lstOption = new List<SelectOption>();   if(strCountryName != null && strCountryName == ‘IN’) { lstOption.add(new SelectOption(‘DL’, ‘Delhi’)); lstOption.add(new SelectOption(‘SRE’, ‘Saharanpur’)); lstOption.add(new SelectOption(‘MZN’, ‘Muzzafarnagar’)); } else if(strCountryName != null && strCountryName == ‘US’) {   lstOption.add(new SelectOption(‘CO’,’Colorado’)); lstOption.add(new SelectOption(‘NE’,’Nevada’)); lstOption.add(new SelectOption(‘TE’,’Texas’)); } else { lstOption.add(new SelectOption(‘None’,’— Select —‘)); } return lstOption; } }
  • “Save” this code.
Step 3- Check it and make sure it’s working correctly! So Now it will look something like this images- Creating dependent picklist salesforce within Salesforce are not very hard to. It is something that is very useful and can be used to make your life easier. We hope this has helped someone that is having trouble developing this within Salesforce. Thank You!
Enquire Now

lets get over a cup of coffee and discuss!

Page Name: