Sub topic 5
Handling blank values correctly
This chapter assumes you can create a validation rule in Setup, you know the Salesforce data model, and you can read spreadsheet-style formulas. It covers how blank values behave in validation rule formulas and how to avoid the most common mistakes.
Why blanks need attention
A validation rule formula returns true when the data is invalid. Many fields are empty at save time, either because the user left them empty or because the record never had a value. If your formula does not account for that, it can block saves it should allow, or let bad data through.
Two questions come up every time you write a rule:
- Should an empty field count as invalid, or should the rule be skipped?
- Does the formula still evaluate safely when the field is empty?
What "blank" means in a formula
Formulas treat empty values differently by field type.
| Field type | Empty value looks like | Safe test |
|---|---|---|
| Text, Text Area, Email, Phone, URL | Empty string | `ISBLANK(field)` |
| Number, Currency, Percent | No value (null) | `ISBLANK(field)` |
| Date, Date/Time | No value (null) | `ISBLANK(field)` |
| Picklist | No value selected | `ISPICKVAL(field, "")` |
| Checkbox | Never blank, it is true or false | `field = TRUE` or `NOT(field)` |
| Lookup | No related record | `ISBLANK(LookupField__c)` |
Picklists are covered in the next chapter. The key point here is that ISBLANK does not work on picklist fields.
Using ISBLANK
ISBLANK(expression) returns true when the expression has no value. The formula below flags an Opportunity that has no Amount.
ISBLANK(Amount)Use NOT(ISBLANK(...)) when you want to act only if a value exists.
AND(
NOT(ISBLANK(Description)),
LEN(Description) < 20
)This rule fires only when a description exists and is shorter than 20 characters. An empty description passes. This is a common pattern: a field is optional, but if the user fills it in, the value must meet a standard.
ISBLANK versus ISNULL
Older formulas use ISNULL. Avoid it in new rules. For text fields, ISNULL does not treat an empty string as empty in some contexts, so it can return false for a field that looks empty. ISBLANK handles both null and empty strings, and Salesforce recommends it for all field types except picklists.
Blank numbers are not zero in validation
Salesforce has an option on formula fields for how to treat blank number fields: as zero or as blank. For validation rules, a blank number is still blank. That means comparisons behave in ways that surprise people.
Look at the first code example from the plan.
AND(
ISPICKVAL(StageName, "Negotiation/Review"),
OR(ISBLANK(Amount), Amount <= 0)
)The rule needs both parts of the OR. If you wrote only Amount <= 0, a blank Amount might not trigger the error, because a comparison against a null number does not reliably evaluate as true. Testing ISBLANK(Amount) first makes the intent explicit: blank and zero or negative are both invalid at this stage.
The inverse also matters. If a rule should apply only when a number exists, guard it.
AND(
NOT(ISBLANK(Discount_Percent__c)),
Discount_Percent__c > 30
)Here a blank discount passes, and a value above 30 fails.
Required only under some conditions
Setting a field as required on the page layout or the field definition makes it required everywhere. A validation rule lets you require a field only when some condition holds. This is one of the most common uses of ISBLANK.
AND(
ISPICKVAL(StageName, "Closed Lost"),
ISBLANK(Loss_Reason__c)
)If you write this rule with the error location set to the Loss_Reason__c field, the user sees the message right beside the field they must fill in.
You can combine several conditions. This rule requires a contract end date when a contract start date exists.
AND(
NOT(ISBLANK(Contract_Start__c)),
ISBLANK(Contract_End__c)
)Two fields that depend on each other
Sometimes either both fields must be filled in or both must be empty. The expression below fires when exactly one of them is blank.
XOR(
ISBLANK(Contract_Start__c),
ISBLANK(Contract_End__c)
)XOR returns true when exactly one of its arguments is true. It is a compact way to express "all or nothing" without writing out two AND branches.
For "at least one of these must be filled in", use AND over the blank tests.
AND(
ISBLANK(Phone),
ISBLANK(Email)
)This fires when both are empty, so a user must provide at least one.
Whitespace
A text field that contains only spaces is not blank. Salesforce usually trims leading and trailing spaces when a user saves through the standard interface, but data from an integration or an API call can differ. If whitespace matters, use TRIM.
ISBLANK(TRIM(Name__c))Do not rely on this as a general safeguard. Test how your own data sources behave before you decide whether you need it.
Blank-safe formulas and errors
Some formula functions fail when given a blank. A rule that errors at save time can block users with a confusing message, or the rule may not save at all.
Common cases:
REGEXon a blank text field returns false for most patterns, which would makeNOT(REGEX(...))true and flag a blank value as invalid. Guard withNOT(ISBLANK(...))first.- Math on a blank number, such as dividing by a field, can produce an error. Guard the divisor.
- Cross-object references return blank if the lookup is empty. A comparison on that blank value may behave differently from what you expect.
The postal code example shows the first guard.
AND(
BillingCountry = "USA",
NOT(ISBLANK(BillingPostalCode)),
NOT(REGEX(BillingPostalCode, "\\d{5}(-\\d{4})?"))
)Without the NOT(ISBLANK(...)) line, an Account with a US country and no postal code would be rejected by the format check. Whether that is correct is a business decision. If the postal code is required, say so with a separate rule and a separate message. One rule per requirement keeps messages specific and makes troubleshooting easier.
Guarding a division:
AND(
Units__c <> 0,
NOT(ISBLANK(Units__c)),
Total_Cost__c / Units__c > 500
)Because AND in Salesforce formulas evaluates all arguments rather than stopping early, do not assume the first test protects the later ones. In this case, use IF to keep the division from running at all.
IF(
OR(ISBLANK(Units__c), Units__c = 0),
FALSE,
Total_Cost__c / Units__c > 500
)Here the IF returns FALSE (valid) when units are blank or zero, and only divides otherwise. The same technique applies to any expression that can fail on a blank.
Deciding how to treat a blank
Before you write a rule, decide which of three behaviors you want.
| Goal | Pattern |
|---|---|
| Blank is invalid | Include `ISBLANK(field)` in the error condition |
| Blank is allowed, value must meet a standard | Add `NOT(ISBLANK(field))` as a guard |
| Blank is invalid only in some circumstances | Combine the condition with `ISBLANK(field)` using `AND` |
Common mistakes
- Using
ISBLANKon a picklist. It is not valid for picklist fields. UseISPICKVAL(field, ""). - Writing
field = ""for numbers or dates. UseISBLANK. - Forgetting the guard before
REGEXor a comparison. A blank value then fails the format check. - Assuming
ANDshort-circuits. UseIFwhen a later expression could fail. - Treating a checkbox as blank. A checkbox is always true or false. Test it directly.
- Combining unrelated checks in one rule. The user gets one generic message and cannot tell what to fix.
Summary
Blank values are a normal state for most fields, so each rule must say what a blank means. Use ISBLANK for most field types, ISPICKVAL(field, "") for picklists, and guards with NOT(ISBLANK(...)) before pattern or math checks. Use IF when an expression can error on a blank, and keep each rule focused on one requirement.
Exercise
An Opportunity must have a CloseReasonc text field filled in when StageName is "Closed Lost". Separately, if a user enters DiscountPercent__c, it must not exceed 30, but leaving it blank is fine. Write the error condition formula for each of the two rules.
Show answer
Rule 1: AND(ISPICKVAL(StageName, "Closed Lost"), ISBLANK(CloseReasonc)). Rule 2: AND(NOT(ISBLANK(DiscountPercent_c)), DiscountPercent_c > 30). Rule 1 uses ISPICKVAL because StageName is a picklist, and ISBLANK because CloseReason__c is text. Rule 2 guards the comparison so a blank discount passes.
Exercise
A rule uses NOT(REGEX(Phone, "\\d{10}")) alone as its error condition. Users complain they cannot save records that have no phone number. Why does this happen, and how do you fix it if blank phones should be allowed?
Show answer
A blank value does not match the pattern, so REGEX returns false and NOT makes the condition true, which blocks the save. Add a guard: AND(NOT(ISBLANK(Phone)), NOT(REGEX(Phone, "\\d{10}"))).