Sub topic 6
Validating picklist fields
This chapter assumes you can write a basic error condition formula, where true means the record is invalid. It also assumes you know how AND, OR, NOT, and ISBLANK work. Picklist fields need extra care because formulas treat them differently from text fields.
Why picklists are different
A picklist stores one value from a fixed list, but formulas do not treat it as plain text. You can't compare it with =, and most text functions reject it. This formula fails to save:
StageName = "Closed Won"Salesforce reports that the field is a picklist and is only supported in certain functions. The functions that do accept a picklist are ISPICKVAL, TEXT, CASE (with TEXT), ISBLANK on some field types (see the blank section), and PRIORVALUE. Multi-select picklists use INCLUDES instead.
Testing for one value with ISPICKVAL
ISPICKVAL(field, "value") returns true when the field holds exactly that value. The second argument must be a text literal. You can't pass a formula expression or another field.
This rule requires a loss reason when an opportunity is closed lost. Loss_Reason__c is a custom text field used for illustration.
AND(
ISPICKVAL(StageName, "Closed Lost"),
ISBLANK(Loss_Reason__c)
)Two details trip people up:
- Match the stored value. The literal must match the picklist value as stored, including spaces, punctuation, and case differences in the value itself.
"Negotiation/Review"works, and"Negotiation Review"never matches. No error is raised, so the rule silently does nothing. - Translations don't matter. If your org uses the Translation Workbench, the formula compares against the master value, not the label a translated user sees. Write formulas against the master value.
Testing for several values
To test for one of several values, you have three choices.
OR(
ISPICKVAL(Status, "Escalated"),
ISPICKVAL(Status, "On Hold"),
ISPICKVAL(Status, "Closed")
)This form is explicit and works for any number of values, but it gets long.
CASE with TEXT is shorter when the list is large. CASE compares an expression to each value and returns the matching result, or the last argument if nothing matches:
CASE(TEXT(Status),
"Escalated", 1,
"On Hold", 1,
"Closed", 1,
0) = 1The third option, CONTAINS, is shorter still but risky:
CONTAINS("Escalated;On Hold;Closed", TEXT(Status))CONTAINS returns true when the second argument is an empty string, so a blank picklist passes this test. It also matches partial strings. Avoid it unless you have guarded for blank values, for example by adding NOT(ISPICKVAL(Status, "")).
Handling blank picklists
A picklist that is not required can be empty. The reliable test is ISPICKVAL(field, ""):
AND(
ISPICKVAL(Type, ""),
Amount > 10000
)ISBLANK does not work on picklist fields in validation rules. If you need to compare text, TEXT(field) = "" is equivalent.
A rule that depends on a picklist value should usually also handle the blank case on purpose. A rule like NOT(ISPICKVAL(Type, "Customer")) is true for a blank picklist as well as for every other value. If you only want to flag records that have some other value, add NOT(ISPICKVAL(Type, "")).
Using TEXT
TEXT(picklist) converts the selected value to text, which opens up text functions and comparisons:
| Goal | Formula fragment |
|---|---|
| Compare to a literal | `TEXT(Rating) = "Hot"` |
| Compare two fields | `TEXT(Rating) = Custom_Rating__c` |
| Check the length of the value | `LEN(TEXT(Rating)) > 3` |
| Test a blank value | `TEXT(Rating) = ""` |
| Use in a CASE | `CASE(TEXT(Rating), "Hot", 1, 0)` |
ISPICKVAL is the better choice for a single literal value because it reads clearly. Use TEXT when you need to compare a picklist to another field or call a text function.
Choosing the right function
The diagram summarizes how to pick a function for a picklist check.
Multi-select picklists
A multi-select picklist stores selected values joined by semicolons. ISPICKVAL does not work on it. Use INCLUDES(field, "value"):
AND(
INCLUDES(Interests__c, "Golf"),
INCLUDES(Interests__c, "Tennis")
)INCLUDES takes a literal for the second argument, like ISPICKVAL. You can't use it with TEXT. If you need to test that nothing is selected, use ISBLANK(Interests__c), which works for multi-select picklists.
Detecting picklist changes
Combine ISCHANGED with PRIORVALUE to restrict a rule to certain transitions. PRIORVALUE returns the value before the current edit, and you can pass it to ISPICKVAL.
This rule stops anyone from reopening a closed-won opportunity:
AND(
ISCHANGED(StageName),
ISPICKVAL(PRIORVALUE(StageName), "Closed Won"),
NOT(ISPICKVAL(StageName, "Closed Won"))
)ISCHANGED is false on a new record, so this rule only checks edits. If you use PRIORVALUE without ISCHANGED or NOT(ISNEW()), remember that on insert there is no prior value, and the rule can behave in ways you did not intend. Guard it explicitly.
Picklists on related records
You can reach a picklist on a parent record with a cross-object reference and check it the same way:
AND(
ISPICKVAL(Account.Type, "Prospect"),
ISPICKVAL(StageName, "Closed Won")
)This blocks closing an opportunity as won while its account is still a prospect. Cross-object references only go up to parents through lookup or master-detail relationships, and the formula reads the current parent value at save time.
Edge cases to know about
- Inactive values. Deactivating a picklist value doesn't change existing records. A rule that depends on that value still matches old records, which can block unrelated edits to them.
- Unrestricted picklists. An API or data load can write a value that isn't on the list unless the picklist is restricted. A rule can reject unexpected values, but it's usually simpler to restrict the picklist.
- Record types. A record type limits the values shown in the UI, but it does not change what a formula sees. If a value is only valid for one record type, check
RecordType.DeveloperNamein the rule. - Dependent picklists. Dependencies are enforced in the UI. The API and imports can sometimes bypass them, so add a rule when the combination must always be valid.
- Default values. A default picklist value fills in on a new record, so a rule that expects a blank value may never see one.
Putting it together
This rule requires a close date no later than today for a won opportunity and requires a non-blank amount. It shows the standard pattern: one ISPICKVAL guard, then the conditions that make the record invalid.
AND(
ISPICKVAL(StageName, "Closed Won"),
OR(
ISBLANK(Amount),
CloseDate > TODAY()
)
)Exercise
Write an error condition formula for a Case that blocks saving when Status is "Closed" and Priority is blank. Which function do you use for the blank test, and why not ISBLANK?
Show answer
AND(ISPICKVAL(Status, "Closed"), ISPICKVAL(Priority, "")). ISBLANK does not work on picklist fields in validation rules, so ISPICKVAL(Priority, "") (or TEXT(Priority) = "") is used to test for an empty picklist.
Exercise
A rule uses CONTAINS("Hot;Warm", TEXT(Rating)) and unexpectedly flags records with no rating. Why does this happen, and how do you fix it?
Show answer
CONTAINS returns true when its second argument is an empty string, and TEXT of a blank picklist is empty. Fix it by adding NOT(ISPICKVAL(Rating, "")) to the formula, or switch to OR(ISPICKVAL(Rating, "Hot"), ISPICKVAL(Rating, "Warm")).