How To Create Dynamic Dependent Picklist Of Objects Within Salesforce

How To Create Dynamic Dependent Picklist Of Objects Within Salesforce

By admin December 9, 2013
Blog, Salesforce Development 3

Another question has been asked to our Salesforce developers so we thought best to share it with the Salesforce development world. Dependent picklist 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.

Displaying dependent picklists is very simple with a Visualforce page. Let’s assume that we have custom objects called continents, country, state, region and major cities. These are all the objects that are related to each other by the Lookup field within Salesforce.

In this simple example we are going to make 5 different objects within Salesforce. These will be continents, country, state, region and major cities. We will create 4 lookup fields as well. The first lookup field in country will be linked with continent, the second lookup field is state and is linked with country. The third lookup field is region and is linked with state and so on.

If you follow these simple steps within Salesforce, you can create a dynamic dependent picklist:

sample Image

Step 1 –

Go To Setup a Develop a Apex Classes

Click the “New” button for creating new Apex class:

Copy this code and paste it in your editor

public class MultilevelDependentPicklist {public string conti{get; set;}public string contr{get; set;}public string stat{get; set;}public string regi{get; set;}public string city{get; set;}public MultilevelDependentPicklist(ApexPages.StandardController controller) { }public list getcontinentobj(){list options= new list();list conti= [select name from Continent__c order by name];options.add(new selectoption(‘—-Select Anyone—-‘, ‘—-Select Anyone—-‘));for(Continent__c c:conti){options.add(new selectoption(c.name,c.name));}return options;

}

public list getcontryobj(){

list options= new list();

list contr= [select name  from Country__c where Continent__r.name=:conti];

options.add(new selectoption(‘—-Select Continent—-‘, ‘—-Select Continent—-‘));

for( Country__c c:contr){

options.add(new selectoption(c.name,c.name));

}

return options;

}

public list getstateobj(){

list options= new list();

list stat= [select name from State_Provice__c where Country__r.name=:contr];

options.add(new selectoption(‘—-Select Country—-‘, ‘—-Select Country—-‘));

for(State_Provice__c s:stat){

options.add(new selectoption(s.name,s.name));

}

return options;

}

public list getregionobj(){

list options= new list();

list regi= [select name from County_Region__c  where State_Provice__r.name=:stat];

options.add(new selectoption(‘—-Select State—-‘, ‘—-Select State—-‘));

for(County_Region__c r:regi){

options.add(new selectoption(r.name,r.name));

}

return options;

}

public list getcityobj(){

list options= new list();

list city= [select name from City__c where County_Region__r.name=:regi];

options.add(new selectoption(‘—-Select Region—-‘, ‘—-Select Region—-‘));

for(City__c c:city){

options.add(new selectoption(c.name,c.name));

}

return options;

}

}

 

Step 2 –

Go To Setup a Develop a Pages

Click the “New” button for creating new Visualforce pages within Salesforce

image3

Step 3 –

In the label and name box type “Multi-level Dependent Picklist” within Salesforce

image4

Step 4 –

In the Visualforce page editor paste this following code:

“Save” this code.

Step 5-

Check it and make sure it’s working correctly. Enjoy!

Creating dependent picklists 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. If you have any other Salesforce development questions or need anything else. Please feel free to reach out to the Mind Digital team anytime.

Related Posts

Popular post