Introduction
It
is a great experience to see as an admin the power of salesforce.This platform
has taught me how to build complex formulas using UI and solve business
problems leveraging existing salesforce functionalities. Recently business approached
me with a requirement and after reviewing the details, I was not sure if it is really
possible by creating a simple formula from UI until I came across the robotic
regex function. I am calling it a robot because it is capable of carrying out a complex series of actions automatically. Having realized
it is a solution for the requirement I thought I would share this on my blog to
help admins and developers in achieving similar use case. This blog introduces how to
use regex function in building advanced formulas
Regex Function
A
regular expression which is also known as Regex is a text string that describes
a pattern to search, edit, find positions in a body or manipulate text and data.
You define the patterns and the regex function simply goes and matches it in
the string. I love regex because with such tiny expressions you can write
complex validation rules, workflow rules and formulas in salesforce. It is
worth investing little bit of time learning this expression to make your life
easier while working in salesforce.
I
don’t really know where to start in terms of how huge and significant this expression
is. Let us begun with a use case
Use Case:
123
SFDC corporation has lead traffic coming into salesforce from various servers. These
servers have relaxed validation rules due to which leads are coming into
salesforce with missing phone and email information. If country is United
States than salesforce should not accept leads that have both email and phone
field blank. If email is blank than phone number field should contain minimum 4
numbers. Phone number field can also contain upper and lower case alphabets,
dashes, brackets etc.
We
are basically trying to limit or control the data a user can enter on a lead
record before saving it. This can be achieved by creating a validation rule. A
validation rule can contain a formula expression that evaluates the data in
field and returns a value of “True” or “False”. When the rule returns a true
value then an error message pops up.
Extract
of high level requirements:
- New lead records and country is United States
- Both Phone and email cannot be blank
- If email is blank than phone should contain minimum 4 numbers
Let us break down the validation rule step by step:
Step1:
We want this rule to work only for new leads i.e. every time a new lead record is created in
salesforce. This rule is not applicable for existing records.
Use ISNEW () function, AND function checks whether all arguments are true and returns TRUE if all arguments are true
AND
(ISNEW(),
Step2:
Validation rule should trigger if country is US, use country =
‘United states’ in teh formula.
Step3:
Phone or email cannot be blank. Use ISBLANK function.
ISBLANK
checks whether an expression is blank and returns true or false value.
ISBLANK (Email),
ISBLANK (Phone)
Step4:
If email is blank than phone number should contain minimum 4 numbers.
Use REGEX function
NOT REGEX(Phone, "[\\w\\d\\s)(-]*[\\d]{1}[\\s\\w\\d)(-]*[\\d]{1}[\\s\\w\\d)(-]*[\\s\\d]{1}[\\s\\w\\d)(-]*[\\d]{1}[\\s\\w\\d)(-]*")
[
] - Match anything inside the square bracket
Dash- Inside the square bracket is the range separator and allows us to define a range
\\ - escaped backslash for re.compile ( pattern, flags = 0)
\s - Match any whitespace characters (space, tab
etc.).
\w - Match any character in the range 0 - 9, A - Z and
a - z
\d - Match any character NOT in the range 0 - 9
)(- Field can contain bracket and dashes
* - Zero or more times
No comments:
Post a Comment