Sub topic 3
Operators and everyday functions
This chapter assumes you can create a formula field in Setup and know the basic return types (Text, Number, Date, Checkbox). It covers the operators and functions you will use in most formulas. Branching with IF and CASE comes in the next chapter.
Operators
Operators combine values into an expression. Salesforce formulas use these groups.
| Group | Operators | Example | Column | Column |
|---|---|---|---|---|
| Math | `+` `-` `*` `/` `^` | `Amount * 0.1` | ||
| Comparison | `=` `==` `<>` `!=` `<` `>` `<=` `>=` | `Amount >= 10000` | ||
| Logical | `&&` ` | ` `!` | `Amount > 0 && IsClosed` | |
| Text | `&` | `FirstName & " " & LastName` |
The & operator joins text. The + operator also joins text, but & is clearer because it never adds numbers by accident. Text comparisons with = are not case-sensitive.
Everyday functions
A function takes inputs and returns a value. These cover most day-to-day needs.
| Function | Purpose | Example |
|---|---|---|
| `BLANKVALUE(expr, fallback)` | Use a fallback when a field is empty | `BLANKVALUE(Amount, 0)` |
| `TEXT(value)` | Convert a number, date, or picklist to text | `TEXT(Amount)` |
| `ISPICKVAL(field, "value")` | Test a picklist value | `ISPICKVAL(StageName, "Closed Won")` |
| `ISBLANK(field)` | Test whether a field is empty | `ISBLANK(CloseDate)` |
| `LEN(text)` | Count characters | `LEN(Name)` |
| `ROUND(number, places)` | Round a number | `ROUND(Amount / 3, 2)` |
Picklist fields need special care. You cannot compare a picklist field to a string with =. Use ISPICKVAL, or wrap the field in TEXT.
Worked example
Suppose you want a Text formula field on Opportunity that labels each deal by size. An empty Amount would make the comparison unreliable, so BLANKVALUE treats it as 0 first.
IF(BLANKVALUE(Amount, 0) > 100000, "Large",
IF(BLANKVALUE(Amount, 0) > 10000, "Medium", "Small"))Read it from the inside out. BLANKVALUE(Amount, 0) returns the amount, or 0 if it is empty. The outer IF checks for Large. If that fails, the inner IF checks for Medium, and anything else is Small. The next chapter explains IF in detail.
Here is how Salesforce evaluates the expression.
Common errors
- Mismatched types.
"Total: " & Amountfails becauseAmountis a number. Write"Total: " & TEXT(Amount). - Empty numbers in math. In a formula field, an empty number is treated as 0 only if you set the blank-field handling option to do so. Use
BLANKVALUEto be explicit. - Wrong return type. The formula result must match the return type you chose. A Text formula cannot return a bare number.
Exercise
Why does IF(StageName = "Closed Won", 1, 0) fail to save, and how do you fix it?
Show answer
StageName is a picklist, and picklists cannot be compared to a string with =. Use IF(ISPICKVAL(StageName, "Closed Won"), 1, 0).
Exercise
Write an expression that joins an Opportunity's Name and its Amount into one Text value, such as "Acme Deal: 5000".
Show answer
Name & ": " & TEXT(Amount). The TEXT call is required because & joins text and Amount is a number.