Hi there Flow Friends! In my last post I talked about validating user inputs. Several folks sent me messages asking about validating inputs that don’t have built-in validation or making the validation dependent on multiple selections. We can do that by building our own validation process using a decision loop. Let’s get started!
Here’s an overview of our validation using a decision element. In a nutshell, we’re going to prompt the user for input -> check if the input is valid using a decision element -> if not valid, set an error message -> go back to user input and display the error message.
Use Case #1: If opt in, require email.
In our flow we have a screen where we prompt if the user wants to subscribe to our newsletter. If they check Yes, we need to require they enter a valid email address. We can setup validation on the email input using regex (see previous post) so that if they enter an email they have to enter a valid one. BUT we can’t make the email input a required input, because it is only required if the checkbox is true.
What we can do is check if both the checkbox = true AND the email input is empty using a decision element. If the checkbox is true and the email input is blank we’ll send them back to the input screen and make them try again, otherwise we’ll continue on with the flow.
Since we don’t want our users to be confused, we’ll display an error message telling them what the problem is. For the error message, create a text variable called ErrorMsg and set the default to !$GlobalConstant.EmptyString. Use a DisplayText to display the ErrorMsg on the user screen. Since the variable is blank, nothing will display on the screen until we give it a value.
In this case, if the flow goes down the “No Email” path, we’ll set the ErrorMsg to “Error: Must enter email if Subscribe to Newsletter is Checked” using an assignment like so:
Put it all together and if the user checks “Subscribe to Newsletter” but fails to enter a valid email, they’ll get a screen like this:
And that’s it man. A simple but effective technique to validate input in Flow when the built in options won’t work for you. Is your brain starting to churn out ideas on how you could use this in your Flows? Let’s look at a couple more use cases.
Use Case #2: Validate number input against a picklist
We have a requirement where we need to prompt for the size of rental property in terms of bedrooms and how many people will occupy the unit. We’ll prompt the user for the number of occupants using a number input and have them select the number of bedrooms using a picklist. Out rental rules state that we can have a maximum of two people per bedroom.
We’re going to use the same methodology as the first case, but we’re going to use a formula in our decision. I created four choices for our Bedrooms picklist; 1 Bedroom, 2 Bedroom, 3 Bedroom, and 4 Bedroom. For each choice I set the choice’s “Stored Value” as a number. This way we can use stored value in out formula.
Our formula needs to divide the number of occupants by the number of bedrooms. If the result is greater than 2 then we have exceeded the maximum number of occupants per bedroom. Let’s set the formula up as a boolean so it’s easy to use in our decision.
So if the number of occupants per bedroom is 2 or less, the formula returns true, otherwise it returns false. Then we just setup our decision so that if the formula evaluates to true we continue with the flow, otherwise we send the user back to the input screen.
Use Case #3: Must pick n items from Multi-Select picklist
So you’ve ignored Steve Molis’ advice and are using a multi-select picklist in your org and now your flow has to deal with it. To make matters more difficult, you have a requirement to present the user with a list of choices and they must select at least two choices. What’s a poor Flow Developer to do?
One of the problems dealing with a multi-select is the value returned is just a semi-colon delimited string. If a user selects Red, White and Blue for their choices you get ‘Red; White; Blue’ back. But maybe we can use this to our advantage. All we have to do is count the number of semi-colons and add 1 to figure out how many items are selected (we have to add 1 because there is no trailing semi-colon). Once we know the number of selections we can create a decision for it.
Here’s my Multi-select list. I’ve done something a little sneaky here and put the ErrorMsg variable in the input label. This just keeps the error message with the input that caused it rather than having to add a separate displaytext field.
There’s no easy way to count the number of semi-colons directly, but we can get there indirectly. We can take the length of the string X, remove the semi-colons using the Substitute function, and subtract the new string length from the original string length X. So for example, if the string length X is 20 characters with 3 semi-colons (length X=20), we remove the semi-colon by substituting them with “”, and take the length of the string now (length Y=17). Subtract Y from X and we get 3, so we must have had 3 semi-colons. We add 1 because there is no trailing semi-colon, so the original string must have had 4 entries. Here’s the resulting formula:
Once this is all setup, it’s a simple matter of setting up our decision element using our formula. In this particular case, if they select less than two choices I’m going to send them back to the input screen.
That’s it for now Flow Friends. I love getting your feedback and questions, so hit me with a comment. Happy flow’in.