Update June-24 2018 : Refer this blog post to create a lookup component in Flow with better user experience.
Flow is pretty powerful tool not only for admins but for developers as well. As a best practice, we should not code until and unless there is no “better” point and click option available. If you have to solve one problem using 20+ workflow rule and it could be solved in just 10-20 lines of code then off-course in those scenario it makes sense to use code over point and click.
You can consider visual flow equivalent to Visualforce for admin. We can create very powerful wizard like functionality without any code. Once flow is implemented, we can use it in Custom button, Visualforce page, chatter actions etc…
I was in situation where I needed to create a lookup field, however lookup field element is not available in Flow. Lets consider we want to search Lead record just like lookup window functionality and display matching result to end users. As lookup field is not available, we will provide list of options to end user using “Dynamic Record Choice” and once user select record we will make selected record available on next screen.
- Let’s create a new Flow by name “LookupDemo”.
- Drag an element “Screen” from tool box available on left to flow canvas.
- Name it “Enter Lead Name to search”.
- In pop up window of screen, choose “Add a field” tab.
- Drag a “Textbox” field to right portion of screen popup.
- Label it “Lead Name” as shown in below image.
- Click on “Ok” button once done and mark Screen as “Start element” by clicking on green arrow appears when cursor is hovered over screen element.
- We need to create two variables to hold selected Lead Name and Lead Id.
- Navigate to “Resources” tab in tool box and select Variable.
- Create two variable by name “varLeadId” and “varLeadName” of type “text” and Input/output Type as “Input and Output”. It is best practice to prefix variable with “var” to avoid confusion.
- Now, drag a new Screen element and name it as “Matching Lead records”
- Add a field “Radio Buttons” from “Add a field” tab to right portion of screen popup
- Name this newly added “Radio Buttons” field to “Matching records”.
- In “Choice Settings” area of Field property, choose “Dynamic Record Choice” as shown in below image.
- As we select “Dynamic Record Choice”, new popup window will appear.
- Name it as “MatchingLeadChoices”.
- We want to save Lead Id as a value in background so choose “Value Data type” as Text.
- Choose “Lead” in “Create a choice for each” as we want to search Lead Object for records.
- As we select “Lead”, remaining portion of popup screen will be rendered.
- We need to add condition for records to appear on this screen. Select “Name” in field, “contains” in operator, “Lead_Name” screen input field in Value. This means, we will search all lead record which has text entered in previous screen in name field of existing lead records.
- Now move to next part of page and select “Name” in Choice Label.
- We could be in tricky situation if end user wants to see more than one field, like in Lookup search window, multiple columns are available. In these scenario, we need to create new Formula filed with combination of all fields which end user wants to see and use that field in “Choice Label”.
- Select “Id” in Choice Stored Value.
- I would suggest to limit records to 100 or less if many matching record exists in Salesforce Org and it can be sorted as well in those cases.
- In next section named “Additional Options”, set Name and Id field to variables “varLeadName” and “varLeadId” as shown in below image.
- Connect both Screens by dragging arrow from Screen “Enter Lead” to “Matching Lead records”.
- Drag new Screen to display selected Lead record.
- Name it “Selected Lead”. This screen is just to demonstrate that how selected record can be referred, instead of this screen any other part of our flow can be connected.
- Add 2 field of type “Display Text” to show variable “varLeadName” and “varLeadId” set in previous screen.
- Connect screen “Matching Lead records” to “Selected Lead”.
Final flow will look like
Working print screen, it can be tested by clicking on “run” button flow canvas.