Avoid These Common Mistakes In Your Next Salesforce Project Discovery
Nov 17th, 2021
Regex, or regular expression, is “a string of text that allows you to create patterns that help match, locate, and manage text” ( www.techopedia.com). Originating as a mathematical concept, regular expression is now commonly used in search engines, text editors, and word processors. This string of text, which looks like jibberish to the untrained eye, defines the pattern to compare against a target piece of text.
Using a syntax consisting of metacharacters (characters that have special meaning) and regular characters (literal values like ‘a,’ ‘b,’ etc.) regex can define a search pattern. For example, the expression [a-z] defines a pattern that matches only lowercase alpha characters from a-z. Both ‘a’ and ‘z’ are regular characters, and ‘–‘ is a metacharacter representing ‘between.’
Salesforce provides a regex function that is extremely helpful when composing validation rules. The use of regex allows short, effective validation rules that are precise and would otherwise take much more effort to implement.
Here’s the formula syntax:
REGEX(text, regex_text)
Here are some common ways regex can be used:
Phone and email validation
Enforcing case-sensitive entries
Enforcing specific syntax on Identification Numbers
Here are some examples of how you can apply regex in a validation rule:
REGEX(SSN__c, “[0-9]{3}-[0-9]{2}-[0-9]{4}”)
Here we have a custom field to store SSN (Reminder: Always ensure sensitive data is adequately protected). The function defines the pattern that we will use to evaluate the string of text. It is indicated that it will accept any number between 0-9, provided it occurs {3} times only, followed by a literal hyphen, followed by another set of numbers, this time occurring only {2} times, another regular character hyphen and finally another set of numbers not to exceed a length of {4}
AND(
OR(BillingCountry = “USA”, BillingCountry = “US”),
NOT(REGEX(BillingPostalCode, “\\d{5}(-\\d{4})?”))
)
This validation rule enforces a format for US Postal Codes, specifying that it must fit within a 55555 or 55555-5555 format. If the specified pattern is not found, the validation rule returns your error to the user.
Regex can seem overwhelming. No one expects you to memorize the syntax by heart! There are some great tools out there to help.
Use RegExr to validate and test your regex. RegExr provides a great interface that breaks down the syntax of your regex and allows you to see how it reacts to your text string.
Also check out this syntax table ‘Cheat Sheet.’
As always, take a look at Salesforce Formula documentation.
Author: Grant Ongstad, Senior Salesforce Consultant
EightCloud wildly exceeded our expectations. They showed a genuine commitment to understanding our needs, our business structure and processes. Additionally, they had insights that added value well above the tactics of our implementation of Salesforce. They were a true partner and have helped improve our processes overall."
- Paul Johnson, Guild Mortgage
Partners and friends, we are proud to share the new EightCloud brand and website with you. Our transformation reflects our commitment to helping you achieve Salesforce excellence through a focus on efficiency, expertise and genuine partnership.
We wish you the best and look forward to connecting with you soon!