Validating Input
- Using Flow Validation
We can validate the input in the flow, just like we can with Validation Rules.
However: There is an important difference!
- When we create standard Salesforce Validation Rules we write the logic for the invalid input.
- When we create Validations in Flow the formula is written for the valid input.
The logic is swapped around!
Example: Here is a simple text field. We want a maximum of 10 characters to be entered.Our Flow Validation formula is:
LEN({!text_field}) < 11
Says the LENgth of the text field must be less than 11 to be valid.
In a standard Salesforce Validation Rule, we would have written: LEN({!text_field}) > 10 which would be invalid and cause the error to be displayed.
Here is the configuration of the Flow validation:
Here is the error message that is displayed when we tried to enter 11 characters:Validating a Phone Number
Validating a phone number is a little different, the phone number component gives us a "Pattern" field to configure where we can use a "regex" (Regular Expression) to set the phone number format that is required.
The Pattern used here is: ^[\d]{10,15}
What this means:
- ^ = a marker meaning from the start of the line
- [/d] = digits only
- {10,15} = min 10, max 15 of these chars
Putting it all together, from the start of the line we must have digits only and a minimum of 10 digits and a maximum of 15. No spaces or other characters are permitted.
The Placeholder text: 10-15 digits only displays in the phone number field as a hint to the format required.
This is what the Phone number field looks like now:If we enter too many digits, we get this error:
Address ValidationThe address component has some special configuration fields:
Resulting in:
File UploadThe File Upload component also has some special configuration parameters:
Resulting in:
Exercise: Validate Input in a Screen Flow
Validation in Flow
To practice some of the basic types of validation available in Flow, create a new screen flow and add a Screen component:
Drag the following components across to the canvas:
- 2 x Text
- 2 x Number
- 1 x Phone
Required Field
The simplest validation is to set the field to be required:
String Length
In the next example, you will check that the string length entered is less than 20 characters, using the formula: LEN({!Text_20}) < 20
Note: The API name of this field has been changed to Text_20 make sure you make this change or your formula will fail.
(
Scroll the sidebar to add the Formula configuration section:
Required Number
In the next example, you make a number required by simply ticking the checkbox.
You also allow two decimal places.
Number Validation
Next, you need to make sure that the number is less than 10 and can have up to 2 decimal values.
{!Number_less_than_10} < 10
Scroll the sidebar to add the Formula configuration section:
International Phone Validation
This validation contains a complicated formula, all you really need to do is cut/paste the formula.
The following explanation is provided for those who are curious about what it means:
The formula is expressed in REGEX format and ensures a phone number is entered in international format.
^\+[0-9]{1,4}\s[0-9]{0,3}\s[0-9]{3,4}\s[0-9]{3,4}$
The color coding may help to understand the grouping of characters:
^\+[0-9]{1,4}\s[0-9]{0,3}\s[0-9]{3,4}\s[0-9]{3,4}$
The number must start with +, followed by digits in range 0-9, 1-4 digits (for the country code), a space (\s), digits between 0-9, 0-3 digits (for the area code), a space, digits between 0-9, 3-4 digits (first part of local number), a space, digits between 0-9, 3-4 digits (second part of local number), end of string.
Expression | Meaning |
^ | Start at the beginning of the string |
\+ | A "+" character is required |
[0-9] | Country Code - digits between 0-9 |
{1,4} | Between 1 and 4 digits for country code |
\s | A space character after the country code |
[0-9] | Area Code - digits between 0-9 |
{0,3} | Between 0 and 3 digits for area code |
\s | A space character after the area code |
[0-9] | Digits between 0-9 for the first part of local number |
{3,4} | Between 3 and 4 digits for the first part of local number |
\s | A space character |
[0-9] | Digits between 0-9 for the second part of local number |
{3,4} | Between 3 and 4 digits for the second part of local number |
$ | End of the string |
Insert this formula in the "Pattern" field:
^\+[0-9]{1,4}\s[0-9]{0,3}\s[0-9]{3,4}\s[0-9]{3,4}$
Make the field required too!
The following are phone numbers that should pass validation (note: the spaces are required):
+1 800 5555 6666
+61 7 1234 567
+69 33 555 666
Test these numbers in your flow.
The error messages displayed when you try to enter invalid data are as follows:
Great work, you can now validate input to a flow
No comments:
Post a Comment