Complex validation rules in Salesforce can be a real pain in the butt.

When it’s a single IF statement with perhaps one OR or AND statement, there’s usually no issue. But when it’s a nested statement if ORs followed by ANDs, things can escalate quickly.

Here are some guiding principles that have helped me along the way.

  1. Keep capitalization consistent. Formula fields and constants should be all in uppercase, like IF, AND, OR, NOT, ISBLANK, TRUE, FALSE, etc.
  2. Decide whether you prefer ” || ” vs ” OR ” or ” && ” vs ” AND “, then don’t mix and match.
  3. Put spaces after every “, “, but not between formulas.
  4. Put one condition per line, including AND and OR statements
  5. To help identify where a parenthesis ends, don’t use MS Notepad or Mac equivalent. Instead, find an editor with some developmental logic. I use EditPlus, but there are several good choices available for you.
    1. This means when the cursor is next to a ( or ) or { or }, it automatically highlights the matching brace.

Putting this all together, the results can look something like this

AND(
OR(
INCLUDES(Type, "Carrier (Ground)"),
INCLUDES(Type, "Carrier (Ocean)"),
INCLUDES(Type, "Carrier (Air)"),
INCLUDES(Type, "Carrier (Rail)")
)
, OR(
ISBLANK(BillingStreet),
ISBLANK(BillingCity),
ISBLANK(BillingState),
ISBLANK(BillingPostalCode),
ISBLANK(BillingCountry)
)
)

The takeaway
Design a validation rule syntax and then tame validation rules by applying it.

Category:
Salesforce