Salesforce flow is great alternative for Visualforce pages used by admin. I have written, multiple blog post on Salesforce flow already and in this post we will discuss how we can create dependent picklist in Flow.
If you are already using flow, might know that creating dependent picklist is not supported yet. However we will be taking help of Custom Metadata types to create same behaviour in Salesforce flow.
Why Custom Metadata Type ?
In Summer 15, Salesforce has introduced Custom metadata types on platform. We can use Custom metadata types in place of List Custom settings because of one important benefit “deployment”. Custom Setting and Custom label has always been best friend of developers and admin for a long time to make Salesforce applications more configurable and dynamic. When I say “deployment”, whenever we deploy custom setting between sandboxes and production or distribute packages, records inside custom settings are not moved. We need to take help of API’s or Dataloader to load initial data in Custom setting after deployment. However with Custom metadata type , records are also moved. Setting up Custom metadata type is fairly simple and almost equivalent to List custom setting.
In this blog, we will create dependent picklist of three level. “Country” will be selected and on basis of it, “State” would be displayed and on basis of “state”, “city” would be displayed. Dependent picklists is displayed on same page in standard user interface however in Flow, it would not be possible. For each dependent picklist, we need to have one screen and therefore in our example, we will have total three screens.
First we need to setup Custom Metadata type which will have information needed to create dependency between fields. Navigate to “Setup | Build | Develop | Custom Metadata Types | New Custom Metadata Type”. For this example, we will name custom metadata type as “Dependent Picklist” with following custom fields:
- Object Name for Dependent Picklist (Created on which object)
- Controlling field API Name (API name of controlling field)
- Controlling field Value (Value which will control values in dependent field)
- Dependent field API Name (API name of dependent field)
- Dependent field value (Value which should be displayed on basis of value on controlling field)
Below image shows structure of Custom Metadata type created for this example
Once above structure is created, click on “Manage Dependent Picklists” to create record. Below image shows sample record created in this example
In above image, if we see first record “Trail__c” represents API name of Object on which we are going to create dependent picklist. “Country__c” is API name of field which will control values in dependent picklist. “State__c” is API name of dependent field. “Texas” is value, which would appear when “USA” is selected.
Now, lets start creating Flow by navigating to “Setup | Build | Create | Workflow & Approvals | Flows | New Flow”.
Drag “Screen” component from left to flow canvas. As, this is going to be first controlling field, so we don’t need any extra step here. Simply add “Dropdown List” on screen and in choice settings, choose “Create New | Picklist choice”.
Above setting will populate dropdown field with all options of field “Country__c” from object “Trail__c”.
Lets create two variables first. “varState” to store selected state in dropdown field and “varCity” to store selected city. We don’t need any variable for country because its type is not “Dynamic Record Choice”.
Its time to create a second screen for dependent picklist. This screen will show State of Country selected in previous screen. Drag a dropdown list field on screen two and in choice settings, choose “Create New | Dynamic Record Choice”.
Above screen is self explanatory and very simple. Now, we will move to create last screen which will display “city” on basis of “state” selected in previous screen. Drag a dropdown list field on third screen and in choice settings, choose “Create New | Dynamic Record Choice”.
Final flow will look like
and output will look like
Hope this will help, would appreciate your feedback on this post.